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.
You're actually making use of two different Facebook tools with this flow:
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):
<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.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.