如何在 swift 3 中实现 iOS app link Facebook 功能?

How to implement iOS app link Facebook functionality in swift 3?

我尝试了很多方法来实现这个但都失败了。

我必须在 Facebook 上与 URL 分享信息,当点击 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()

正在运行并成功共享 post。当我单击 link 时,它正在重定向到应用程序,没有太多信息可以重定向到特定的 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 并成功地 post 编辑了信息。但是在单击 link.

时不会重定向到我的应用程序

注意:

我没有网络应用程序。

我的目标是与应用 link 分享 post。单击 link 时,它将打开应用程序并重定向到特定页面(如果已安装应用程序),否则将重定向到 AppStore。那么 link 格式应该是什么,或者我如何构建 link 来实现此功能?

请帮忙。

提前致谢

使用通用链接。如果你想实现这样的事情,使用 universal link 是个好主意。如果安装了应用程序,您可以重定向到应用程序中的特定页面,否则它将导航到应用程序商店。

以下是实现通用 links 的方法,如果您不知道:

Set up universal links - Raywenderlich

Universal links - Appsflyer

注意:如果这不是您要查找的内容,请忽略此内容

我设法使用 Branch.io 获得了相同的结果。这是一个非常强大的应用程序路由 SDK,他们的控制台实际上以一种非常直观和易于理解的方式引导您了解路由方案。

它的工作方式是根据您在控制面板中的设置动态创建具有某些重定向功能的网页。然后,您可以使用生成的网页将其分享到 Facebook 或其他任何地方。

集成方法如下:

  1. 创建帐户here

  2. 安装并link SDK (pod 'Branch')

  3. 使用 Branch 的仪表板设置路由机制(它有指导且非常简单,您只需填写下面的分支)。您可以随时更改图表的外观(例如,如果您希望始终出现在某个网站上)

  1. 在您的应用中初始化共享数据

    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)
    
  2. 生成 link 并在 Facebook 上分享 link

    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()
        }
    
    }) 
    

是的,我通过在服务器端设置元数据实现了这个功能。

参考文献:https://developers.facebook.com/docs/applinks

https://developers.facebook.com/docs/applinks/ios

谢谢...