Using App Links Hosting API for link shared on Facebook from iOS app

前端 未结 3 1870
梦如初夏
梦如初夏 2021-02-03 13:13

I\'ve honestly spent hours on this trying to get it to work. Unfortunately Facebook & App Link\'s documentation is not clear enough. Even the App Links video from F8.

<
3条回答
  •  失恋的感觉
    2021-02-03 14:07

    With the help of MingLi from FB I managed to get it working with the following code:

    - (void)shareToOpenGraphCountdownInvite
    {
        NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={insert your FB app ID here}&client_secret={insert client secret here}"];
        NSString *fullToken = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        NSArray *components = [fullToken componentsSeparatedByString:@"="];
        FBAppAccessToken = [components objectAtIndex:1];
    
        NSDictionary *paramsForAppLinksHost = [NSDictionary dictionaryWithObjectsAndKeys:
                                               FBAppAccessToken, @"access_token",
                                               @"{your app name}", @"name",
                                               @"{your app's custom url}", @"al:ios:url",
                                               @"{app store ID}", @"al:ios:app_store_id",
                                               @"{your app name}", @"al:ios:app_name",
                                               @"{\"should_fallback\": false}", @"web",
                                               nil
                                               ];
    
        [FBRequestConnection startWithGraphPath:@"/{FB app ID}/app_link_hosts"
                                     parameters:paramsForAppLinksHost
                                     HTTPMethod:@"POST"
                              completionHandler:^(
                                                  FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error
                                                  ) {
                                  AppLinksHostURL_ID = [result objectForKey:@"id"]; // store this ID in an NSString
                                  [self postOGStoryWithCustomURL];
                                  if(error) NSLog(@"error = %@", error.description);
    
                              }];
    }
    
    - (void)postOGStoryWithCustomURL
    {
        NSString *urlString = [NSString stringWithFormat:@"https://fb.me/%@/%@", AppLinksHostURL_ID, customURL];
    
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self pathForS3ObjectWithFilename:previewImageFilename]]]];
    
        // Create OG object
        id object =
        [FBGraphObject openGraphObjectForPostWithType:@"timeflyz:countdown_invite"
                                                title:eventBeingShared.eventName
                                                image:image
                                                  url:urlString // fb.me app links hosted url here
                                          description:@"{insert description here}"];
    
        // Create an action
        id action = (id)[FBGraphObject graphObject];
    
        // Link the object to the action
        [action setObject:object forKey:@"countdown_invite"];
    
        // Check if the Facebook app is installed and we can present the share dialog
        FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] init];
        params.action = action;
        params.actionType = @"timeflyz:create";
    
        // 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:@"timeflyz:create"
                                         previewPropertyName:@"countdown_invite"
                                                     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 {
                                                             //                                                         NSLog(@"result %@", results);
                                                             if([[results objectForKey:@"completionGesture"] isEqualToString:@"post"]) {
                                                                 NSLog(@"Posted successfully!");
                                                                 [[NSNotificationCenter defaultCenter] postNotificationName:@"showShareSuccessfullMessage" object:self userInfo:nil];
                                                             } else
                                                                 NSLog(@"Something else happened - user didn't post");
                                                         }
                                                     }];
    
    
        }
    

    Note that "customURL" is an NSString that follows the pattern "?variable1=result1&variable2=result2..."

提交回复
热议问题