I honestly spent hours trying to get it to work. Unfortunately, Facebook and the Application Connection Documentation are not clear enough. Even the App Links video from F8 .
Application Requirements:
- Share the link to FB as an open diagram that users can click to transfer them directly to my application and perform certain tasks (my application needs to get certain parameters from the link).
- Share the link to the FB without entering the FB (i.e., through the "Sharing" dialog) and switch to the native FOS application for iOS, and not to API calls).
Progress achieved:
I use the following code to create a hosted link to the application (since I only have mobile content) on the FB website under the "Publish SDK for iOS" section.
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"{My app name}", @"name", {custom URL}, @"al:iphone:url", @"{app store ID}", @"al:iphone:app_store_id", @"{My app name}", @"al:iphone:app_name", @"{\"should_fallback\": false}", @"web", fbAccessToken, @"access_token", nil ]; [FBRequestConnection startWithGraphPath:@"/{FB app id}/app_link_hosts" parameters:params HTTPMethod:@"POST" completionHandler:^( FBRequestConnection *connection, id result, NSError *error ) { NSLog(@"Result = %@",result); if(error) NSLog(@"error = %@",error); }];
Then I send the OG history to FB (these posts are fine, but without the correct url)
// Create OG object id<FBGraphObject> object = [FBGraphObject openGraphObjectForPostWithType:@"{app name}:{FB object_name}" title:@"Test Link" image:@"https://cdn3.iconfinder.com/data/icons/picons-social/57/56-apple-512.png" // hosted wallpaper with unique id for background url:nil // Assuming I need to put the url to the app link host object here?? description:@"Click to on this test link!"]; // Create an action id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject]; // Link the object to the action [action setObject:object forKey:@"{FB object name}"]; // Check if the Facebook app is installed and we can present the share dialog FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] init]; params.action = action; params.actionType = @"{app name}:{FB action name}"; // If the Facebook app is installed and we can present the share dialog if([FBDialogs canPresentShareDialogWithOpenGraphActionParams:params]) { // Show the share dialog [FBDialogs presentShareDialogWithOpenGraphAction:action actionType:@"{app name}:{FB action name}" previewPropertyName:@"{FB object name}" handler:^(FBAppCall *call, NSDictionary *results, NSError *error) { if(error) { // An error occurred, we need to handle the error // See: https://developers.facebook.com/docs/ios/errors NSLog(@"Error publishing story: %@", error.description); } else { // Success NSLog(@"result %@", results); } }]; }
To handle the incoming URL when someone clicks on a link in the FB OG history, I added the following code to AppDelegate.m according to the FB documentation - see Handling Inbound Links
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { BOOL urlWasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication fallbackHandler: ^(FBAppCall *call) { // Parse the incoming URL to look for a target_url parameter NSString *query = [url query]; NSDictionary *params = [self parseURLParams:query]; // Check if target URL exists NSString *appLinkDataString = [params valueForKey:@"al_applink_data"]; if (appLinkDataString) { NSError *error = nil; NSDictionary *applinkData = [NSJSONSerialization JSONObjectWithData:[appLinkDataString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error]; if (!error && [applinkData isKindOfClass:[NSDictionary class]] && applinkData[@"target_url"]) { NSString *targetURLString = applinkData[@"target_url"]; // Show the incoming link in an alert // Your code to direct the user to the // appropriate flow within your app goes here [[[UIAlertView alloc] initWithTitle:@"Received link:" message:targetURLString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; } } }]; return urlWasHandled; } // A function for parsing URL parameters - (NSDictionary*)parseURLParams:(NSString *)query { NSArray *pairs = [query componentsSeparatedByString:@"&"]; NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; for (NSString *pair in pairs) { NSArray *kv = [pair componentsSeparatedByString:@"="]; NSString *val = [[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [params setObject:val forKey:[kv objectAtIndex:0]]; } return params; }
Could anyone get this job? I still do not understand how the hosted link to the application works, and where to put it (I assume that it should go in the "url" parameter when calling the FBGraphObject openGraphObjectForPostWithType method.
I really do not want to create a website to store all the URLs and add App Link meta tags (I would have to do all this with the application, since each link to the application will be dynamic and unique for each user who generates it from application).
Please, help!