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

房东的猫 提交于 2019-11-28 14:39:45

Use Universal Links. If you want to achieve something like this using universal link is a good idea. You can redirect to a specific page in your app if the app is installed otherwise it will navigate to appstore.

Here is how to implement universal links if you are not aware of:

Set up universal links - Raywenderlich

Universal links - Appsflyer

Note: Please ignore this, if this is not what you are looking for

I managed to achieve the same result using Branch.io. It's a very powerful app routing SDK and their console practically guides you down the routing scheme in a very visual and understandable manner.

The way it works is by dynamically creating a webpage with certain redirecting features according to what you set in the dashboard. You can then use that generated webpage to share it on Facebook or anywhere else.

Here's how you can integrate it:

  1. Create an account here

  2. Install and link the SDK (pod 'Branch')

  3. Set up the routing mechanism using Branch's dashboard (it's guided and pretty straightforward, you just fill in the branches below). You can always change how the diagram looks like (if you want to always end up on a website or so, for instance)

  1. Initialize the share data in your app

    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. Generate link and share the link on 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()
        }
    
    }) 
    

Yes, I achieved this functionality by setting up the metadata in the server end.

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

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

Thanks...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!