使用App Links Hosting API从iOS应用程序在Facebook上共享链接 [英] Using App Links Hosting API for link shared on Facebook from iOS app

查看:196
本文介绍了使用App Links Hosting API从iOS应用程序在Facebook上共享链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我老老实实地花了好几个小时试图让它发挥作用。不幸的是, Facebook & App Link的文档不够清晰。甚至来自F8的 App Links视频

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.

应用要求:


  1. 分享一个FB作为Open Graph故事的链接,用户可以点击它直接将它们带入我的应用程序并执行特定任务(我的应用程序需要从链接接收特定参数)

  2. 分享链接到没有FB登录的FB(即通过共享对话框并切换到本机iOS FB应用程序而不是使用API​​调用)。

到目前为止的进展:

我正在使用以下代码创建托管应用链接(因为我只有移动内容) 发布iOS SDK下的FB开发人员网站。

I'm using the following code to create the hosted App Link (as I only have mobile content) as per the FB developer's website under Publishing iOS SDK.

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
                            ];

/* make the API call */
[FBRequestConnection startWithGraphPath:@"/{FB app id}/app_link_hosts"
                             parameters:params
                             HTTPMethod:@"POST"
                      completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {
                              /* handle the result */
                              NSLog(@"Result = %@",result);
                              if(error) NSLog(@"error = %@",error);
                          }];

接下来我将OG故事发布到FB(这个帖子很好,但没有正确的网址)

Next I post the OG story to FB (this is posts fine but without a 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);
                                                 }
                                             }]; 
}

当有人点击FB OG故事中的链接时处理传入的URL我已根据FB文档将以下代码添加到AppDelegate.m - 请参阅处理传入链接

To handle the incoming URL when someone clicks on the link in the FB OG story I've added the following code to AppDelegate.m as per FB documentation - see Handling incoming 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;
}

有没有人能够使这个工作?我还不清楚托管的App Link如何工作以及放置它的位置(我假设在调用FBGraphObject openGraphObjectForPostWithType方法时它应该放在'url'参数中。

Has anyone been able to get this working? I'm still not clear on how the hosted App Link works and where to put it (I'm assuming it should go in the 'url' parameter when calling the FBGraphObject openGraphObjectForPostWithType method.

我真的不想创建一个网站来存储所有的网址并添加App Link元标记(我必须通过应用程序完成所有这些,因为每个App Link都是动态的,每个都是唯一的在应用程序中生成它的用户。)

I really don't want to create a website to store all the urls and add App Link meta tags (I'd have to do all this via the app as each App Link is going to be dynamic and unique for each user that generates it from with in the app).

请帮助!

推荐答案

在FB的MingLi的帮助下,我设法使用以下代码:

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<FBGraphObject> 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<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[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");
                                                     }
                                                 }];


    }

请注意,customURL是一个NSString遵循模式?variable1 = result1& variable2 = result2 ...

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

这篇关于使用App Links Hosting API从iOS应用程序在Facebook上共享链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆