2017-05-08 41 views
2

我嘗試爲共享鏈接內容添加說明和標題。我使用的吊艙「Facebook分享」(因爲當我使用FBSDKShareKit,說明和標題不建議使用)無法使用Facebook共享鏈接內容爲網址添加說明和標題

https://i.stack.imgur.com/9p9lp.jpg

這是我的代碼「Facebook分享」:

 do{ 
      var myContent: LinkShareContent = LinkShareContent(url: NSURL(string:contentUrl!) as! URL) 
      myContent.description = "Some description" 
      myContent.title = "Hello" 

      let shareDialog = ShareDialog(content: myContent) 
      shareDialog.mode = .shareSheet 
      shareDialog.presentingViewController = self 
      shareDialog.failsOnInvalidData = true 
      shareDialog.completion = { result in 
       // Handle share results 
      } 
       try shareDialog.show() 
      } catch { 
       print("Error: \(error)") 
     } 

但我可以看到說明和標題僅當我使用時: shareDialog.mode = .native 在所有其他情況下(.shareSheet,.Web,.Browser,.FeedWeb,.FeedBrowser)它們不會被添加。我不想使用.native模式,因爲不是每個人都有原生Facebook應用程序。 我的情況是否有解決方案?

**** UPDATE:****

建立適當的meta標籤的網站(這是我分享的網址)本身標題,描述和圖像值以及其他變量上。

回答

0

應當更新myContentshareDialog部分是這樣的:

 let myContent:FBSDKShareLinkContent = FBSDKShareLinkContent() 
     myContent.contentTitle = "Hello" 
     myContent.contentDescription = "Test message" 
     myContent.contentURL = (URL(string: "https://developers.facebook.com")) 

     let shareDialog = FBSDKShareDialog() 
     shareDialog.mode = .shareSheet 
     FBSDKShareDialog.show(from: self, with: myContent, delegate: nil) 

您也可以導入和使用Social庫創建Facebook分享框。並通過Facebook共享委託功能控制您的分享結果。像這樣:

import FBSDKShareKit 
import Social 

class YourViewController: UIViewController, FBSDKSharingDelegate { 
    func facebookShareButtonClicked() { 
     let vc = SLComposeViewController(forServiceType:SLServiceTypeFacebook) 
     vc?.add(UIImage(named: "YourImageName")) 
     vc?.add(URL(string: "https://developers.facebook.com")) //Your custom URL 
     vc?.setInitialText("Your sample share message...") 
     self.present(vc!, animated: true, completion: nil) 
    } 

//Facebook share delegate functions: 
    func sharer(_ sharer: FBSDKSharing!, didCompleteWithResults results: [AnyHashable : Any]!) { 
     print("Success") 
    } 

    func sharer(_ sharer: FBSDKSharing!, didFailWithError error: Error!) { 
     print("didFailWithError: ", error) 
    } 

    func sharerDidCancel(_ sharer: FBSDKSharing!) { 
     print("Sharer cancelled") 
    } 
} 

注意:嘗試用您的設備。它在模擬器上不起作用。並確保Facebook應用程序已安裝您的設備。

+0

我試過使用社會圖書館。它也不幫助我。共享是工作,但不添加初始文本。我正在使用設備。 Xcode 8.2.1,Swift 3.0。可能我必須使用.native模式。 – DmitriyYefremov

+0

我已經更新了我的答案。我用你的方法試了一下。你可以再次嘗試使用我的答案中最重要的代碼嗎? – kkakkurt

+2

我試過了。方法myContent.contentTitle,myContent.contentDescription從Graph API 2.9中棄用。所以我認爲這些方法不被使用。而Facebook從url的元描述中獲得這些字段。 – DmitriyYefremov

1

我有同樣的問題 - 標題,描述和imageUrl在最新的Facebook SDK版本中被棄用和只讀。我從2017年4月剛剛下載了一箇舊的,並且按預期工作。

相關問題