0

I am trying to create posts to facebook from my app using the fb share button. I want this post to link back to the app using applinks. I don't understand how to add data to the facebook url so it knows where to direct the app to when the link is clicked.

FBSDKShareLinkContent * content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:@"https://fb.me/123456789"];
FBSDKShareButton * button = [[FBSDKShareButton alloc] init];
button.shareContent = content;
[self.view addSubview:button];

This works but any query I try to add to the fb url breaks and no longer links to the app. For example:

 content.contentURL = [NSURL URLWithString:@"https://fb.me/123456789?my_view=news"];

Is this he wrong approach? I also tried using the json encoding and al_applink_data but any change to the original url makes the post link to the image url sent and not the app link.

rEquation
  • 77
  • 1
  • 9

1 Answers1

2

You're actually making use of two different Facebook tools with this flow:

  1. The FBSDK inside your app to share a link (looks like you might have got your code example from here?)
  2. Facebook App Links to launch your app when the link is opened.

The process Facebook uses for App Links is actually a bit more complicated...unfortunately it's not quite as simple as just appending a URL parameter. Here's an overview of how it works (source):

  1. You post a link to your content (you're using the FBSDK for this)
  2. The link points at a page that hosts some special App Links meta tags. One of these will look like <meta property="al:ios:url" content="yourapp://path/to/content" /> When Facebook crawls the link and sees these tags, it flags the link as enabled for App Links. You can add these tags to your own site, if you have one and the link points to it, or the Facebook offers the Mobile Hosting API (which it appears you're using) if you don't have a website.
  3. When a user opens this link, your app launches and you'll be passed a URL like this, which your app has to process for itself: yourapp://path/to/content?al_applink_data=JSON_ENCODED_DATA. The redirection to the correct location in your app sadly isn't magic, and assumes you've set up your app to display content based on a URL scheme structure. Facebook offers a walkthrough, but basically you need to add something like this to your AppDelegate.m:

    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation {
        BFURL *parsedUrl = [BFURL URLWithInboundURL:url sourceApplication:sourceApplication];
        if ([parsedUrl appLinkData]) {
            // this is an applink url, handle it here
            NSURL *targetUrl = [parsedUrl targetURL];
            [[[UIAlertView alloc] initWithTitle:@"Received link:"
                                        message:[targetUrl absoluteString]
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
        ...
    }
    

This only covers you for sharing through Facebook though, and doesn't get into the mess that is URL scheme deep links or Apple's new Universal Links in iOS 9. If you want to handle all of those, in addition to Facebook App Links, you could consider a free service like Branch.io (full disclosure: they're so awesome I work with them) to take care of all the technical details for you.

Alex Bauer
  • 13,147
  • 1
  • 27
  • 44
  • this is very helpful - thank you. branch.io looks great and i may use after i make my demo version. i guess the part that is still unclear for me is how can I embed dynamic information from my app in the App Link. If my app has restaurant profiles and a user shares one to fb, I want that link to launch the app and load that restaurant profile. Parsing the json and using the data is no problem, but I need the json to include that particular restaurant. I still don't see how I can add that some how to the shared link to get the info back when it launches the app. Does that make sense? – rEquation Mar 08 '16 at 12:55
  • Ahhh...I edited my answer slightly to clarify how that works. With Facebook, the deep link 'destination' isn't actually inside the JSON data — it's the URL itself. Branch handles things similarly, though URL schemes are no longer required. You might check out this page for a more visual overview: https://branch.io/how-branch-links-work/ – Alex Bauer Mar 08 '16 at 13:26
  • I landed here looking for help with a similar problem. I've set up the Facebook "Share" button, and I want to add a branch.io deep link to the shareContent. How can I do that? – jbm Sep 07 '16 at 22:06
  • @mrwheet you'll probably want to post a new question for this, since it doesn't directly relate to the original topic here. Generally though, if you're using Branch for your links, you can/should skip the Facebook SDK and its Share button, since this would be redundant. – Alex Bauer Sep 07 '16 at 22:12
  • Well, except that using FB's button, I get the native friends list and selection UI for free—and the user gets to feel very much in control (and that they're actually **in** Facebook doing it). ... But yes, I should post another topic. Thanks. – jbm Sep 07 '16 at 23:56