How to implement iOS app link Facebook functionali

2019-01-29 10:17发布

问题:

I have tried many ways to achieve this but failed.

I have to share info on facebook with a URL and when clicked on the url, it will redirect to my app's specific page.

step# 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()

It is working and successfully sharing the post. When I clicked on the link, it is redirecting to the app, not much info to redirect to a particular ViewController.

Step# 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)

Here I am using Open Graph to post and successfully posted the Info. But con't redirecting to my app when clicked the link.

NB:

I don't have web Application.

My goal is to share a post with a app link. When clicked on that link it will open the app and redirect to a specific page if app is installed, otherwise redirect to the AppStore. So what should be the link format or how can I build the link to achieve this functionality?

Please help.

Thanks in advance

回答1:

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



回答2:

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


回答3:

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...