如何在swift 3中实现iOS应用程序链接Facebookfunction?
我已经尝试了很多方法来实现这一目标但失败了。
我必须使用URL在Facebook上分享信息,当点击该url时,它将重定向到我应用的特定页面。
步骤1
let content: FBSDKShareLinkContent = FBSDKShareLinkContent() content.contentURL = NSURL(string: linkUrl) as URL! content.contentTitle = "Test" content.quote = "game" content.contentDescription = "This is a test game to test Fb share functionality" content.imageURL = NSURL(string: imgUrl) as URL! let dialog: FBSDKShareDialog = FBSDKShareDialog() dialog.mode = FBSDKShareDialogMode.native dialog.fromViewController = vc dialog.shareContent = content // if you don't set this before canShow call, canShow would always return YES if !dialog.canShow() { // fallback presentation when there is no FB app dialog.mode = FBSDKShareDialogMode.feedBrowser } dialog.show()
它正在努力并成功分享这篇文章。 当我点击链接时,它会重定向到应用程序,没有太多信息可以重定向到特定的ViewController。
第2步
let properties = [ "fb:app_id": "Fb_id", "og:type": "article", "og:title": "Test", "og:description": "This is a test game to test Fb share functionality", "og:image" : urlImg, "og:url" : linkUrl, ] let object : FBSDKShareOpenGraphObject = FBSDKShareOpenGraphObject.init(properties: properties) // Create an action let action : FBSDKShareOpenGraphAction = FBSDKShareOpenGraphAction() action.actionType = "news.publishes" action.setObject(object, forKey: "article") // Create the content let content : FBSDKShareOpenGraphContent = FBSDKShareOpenGraphContent() content.action = action content.previewPropertyName = "article" FBSDKShareDialog.show(from: vc, with: content, delegate: nil)
在这里,我使用Open Graph发布并成功发布了信息。 但是点击链接后不能重定向到我的应用程序。
注意:
我没有网络应用程序。
我的目标是与应用链接分享post。 单击该链接时,如果安装了应用程序,它将打开应用程序并重定向到特定页面,否则将重定向到AppStore。 那么链接格式应该是什么,或者我如何构建链接以实现此function?
请帮忙。
提前致谢
使用通用链接 。 如果你想通过通用链接实现这样的事情是一个好主意。 如果安装了应用程序,您可以重定向到应用程序中的特定页面,否则它将导航到appstore。
如果您不知道以下是如何实现通用链接:
建立通用链接 – Raywenderlich
通用链接 – Appsflyer
注意:如果这不是您想要的,请忽略它
我设法使用Branch.io获得相同的结果。 它是一个非常强大的应用程序路由SDK,它们的控制台实际上以一种非常直观和易懂的方式引导您完成路由方案。
它的工作方式是根据您在仪表板中设置的内容动态创建具有某些重定向function的网页。 然后,您可以使用生成的网页在Facebook或其他任何地方共享它。
以下是如何集成它:
-
在此创建一个帐户
-
安装并链接SDK(
pod 'Branch'
) -
使用Branch的仪表板设置路由机制(它是引导且非常简单,您只需填写下面的分支)。 您可以随时更改图表的外观(例如,如果您希望始终在网站上结束)
-
初始化应用中的共享数据
let branchUniversalObject: BranchUniversalObject = BranchUniversalObject(canonicalIdentifier: "any ID here") // just for tracking count and analytics branchUniversalObject.title = "some title" // This will appear as the shared content's title branchUniversalObject.contentDescription = "some description" // This will appear as the shared content's description branchUniversalObject.imageUrl = "www.example.com/test.png" branchUniversalObject.addMetadataKey("uid", value: self.userId) branchUniversalObject.addMetadataKey("otherInfo", value: self.otherInfo)
-
生成链接并在Facebook上分享链接
let linkProperties = BranchLinkProperties() linkProperties.channel = "Facebook" linkProperties.feature = "Share" // generate link branchUniversalObject.getShortUrl(with: linkProperties, andCallback: { (shareURL, error) in if error != nil { // handle error } // init Facebook share data let content = FBSDKShareLinkContent() content.contentTitle = "Some title" content.contentURL = URL(string: shareURL) // this URL handles the redirecting according to your dashboard's settings content.contentDescription = "Some description" content.imageURL = URL(string: "www.example.com/test.png") // create Facebook share dialog let dialog = FBSDKShareDialog() dialog.fromViewController = self dialog.delegate = self dialog.shareContent = content dialog.mode = .automatic if dialog.canShow() { dialog.show() } })
是的,我通过在服务器端设置元数据来实现此function。
参考文献: https : //developers.facebook.com/docs/applinks
https://developers.facebook.com/docs/applinks/ios
谢谢…