2017-08-30 94 views
-2

我尝试了下面的代码,但它只打开LinkedIn不共享。如何使用swift 3在linkedin上共享文章?

我正在尝试使用Swift在iOS中集成LinkedIn SDK。

我试过了,但不起作用。请告诉我如何在LinkedIn上分享帖子。

@IBAction func linkbtn(_ sender: Any) { 
    LISDKSessionManager.createSession(withAuth: [LISDK_BASIC_PROFILE_PERMISSION], state: nil, showGoToAppStoreDialog: true, successBlock: {(sucess) in 
     let session = LISDKSessionManager.sharedInstance().session 
     print("Session ",session!) 
     //let url = "https://api.linkedin.com/v1/people/~" 
     if LISDKSessionManager.hasValidSession(){ 
      let url: String = "https://api.linkedin.com/v1/people/~/shares" 

      let payloadStr: String = "{\"comment\":\"YOUR_APP_LINK_OR_WHATEVER_YOU_WANT_TO_SHARE\",\"visibility\":{\"code\":\"anyone\"}}" 

      let payloadData = payloadStr.data(using: String.Encoding.utf8) 

      LISDKAPIHelper.sharedInstance().postRequest(url, body: payloadData, success: { (response) in 
       print(response!.data) 
      }, error: { (error) in 
       print(error!) 

       let alert = UIAlertController(title: "Alert!", message: "aomething went wrong", preferredStyle: .alert) 
       let action = UIAlertAction(title: "OK", style: .default, handler: nil) 

       alert.addAction(action) 
       self.present(alert, animated: true, completion: nil) 
      }) 
     } 
    }) {(error) in 
     print("Error \(String(describing: error))") 
    } 
} 

回答

1

试试这个: -

func shareOnLinkedIn(){ 
    LISDKSessionManager.createSession(withAuth: 
[LISDK_BASIC_PROFILE_PERMISSION,LISDK_W_SHARE_PERMISSION], state: nil, showGoToAppStoreDialog: true, successBlock: {(sucess) in 
     let session = LISDKSessionManager.sharedInstance().session 
     print("Session ",session!) 
     //let url = "https://api.linkedin.com/v1/people/~" 
     if LISDKSessionManager.hasValidSession(){ 
      let url: String = "https://api.linkedin.com/v1/people/~/shares" 

      let payloadStr: String = "{\"comment\":\"YOUR_APP_LINK_OR_WHATEVER_YOU_WANT_TO_SHARE\",\"visibility\":{\"code\":\"anyone\"}}" 

      let payloadData = payloadStr.data(using: String.Encoding.utf8) 

      LISDKAPIHelper.sharedInstance().postRequest(url, body: payloadData, success: { (response) in 
       print(response!.data) 
      }, error: { (error) in 
       print(error!) 

       let alert = UIAlertController(title: "Alert!", message: "aomething went wrong", preferredStyle: .alert) 
       let action = UIAlertAction(title: "OK", style: .default, handler: nil) 

       alert.addAction(action) 
       self.present(alert, animated: true, completion: nil) 
      }) 
     } 
    }) {(error) in 
     print("Error \(String(describing: error))") 
    } 
} 

请确保您已启用w_share许可,您的应用程序设置身份验证选项卡上。并添加Info.plist上的所有必需的值

+0

非常好的解释。 – Prakash

+0

在LISDKAPIHelper.sharedInstance()。postRequest中收到错误。它说没有找到共享实例。你能帮我吗 ? –

+0

您在项目中使用的SDK版本。使用此下载最新版本的SDK: - https://developer.linkedin.com/downloads#iossdk – Pushpendra

0

我知道这是正确的代码,但它没有进入createSession块,所以给出任何建议,为什么发生了,我没有检查身份验证选项卡的权限,但仍然是它dosen't去创建会话

相关问题