【问题标题】:Facebook deep linking with iOS appFacebook 与 iOS 应用程序的深度链接
【发布时间】:2013-12-19 14:38:53
【问题描述】:

我正在使用以下代码发布到 Facebook。它运行良好,但是当我将 MY_URL 替换为 myapp://test_page// 时,帖子不会出现在 Facebook 时间线中。

如果我做错了什么,请告诉我如何将我的应用深层链接到 Facebook 应用。我搜索了几乎所有 *.com 页面和其他 Facebook 开发人员教程,但我无法理解。

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:@"Check out my post in myapp" forKey:@"message"];
[params setObject:MY_URL forKey:@"link"];
[params setObject:MY_IMAGE_URL forKey:@"picture"];
[params setObject:self.strQuestion forKey:@"name"];
[params setObject:@"" forKey:@"caption"];
[params setObject:self.strDescription forKey:@"description"];

[FBRequestConnection startWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST"
 completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
     NSLog(@"FACEBOOK DONE");
 }];

【问题讨论】:

    标签: ios facebook facebook-graph-api deep-linking


    【解决方案1】:

    感谢@Jai Govindani 的回答,但它比您的回答简单得多。我是从 Facebook 文档中发现的。

    首先我更改 Facebook 帐户中的应用设置:

    我在应用程序的设置中添加了 Bundle 标识符。并启用 Facebook deep linking 当然,我还需要 "App store ID"

    并用completionBlock写了下面的方法 -

    - (void)shareOnFacebookWithName:(NSString *)strName withDescription:(NSString *)strDesc withLink:(NSString *)strLink WithPictureLink:(NSString *)strPicLink withCaption:(NSString *)strCaption withCompletionBlock:(shareCompletion)completionBlock
    {
        __block NSString *strLinkBlock = strLink;
    
        [FBSession openActiveSessionWithPublishPermissions: [NSArray arrayWithObjects: @"publish_stream", nil] defaultAudience: FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler: ^(FBSession *session,FBSessionState status,NSError *error)
         {
             if (error)
             {
                 completionBlock(NO);
                 return;
    
                 NSLog(@"error");
             }
             else
             {
                 [FBSession setActiveSession:session];
                 NSLog(@"LOGIN SUCCESS");
    
                 // Put together the dialog parameters
    
                 NSArray* actionLinks = [NSArray arrayWithObjects:
                                         [NSDictionary dictionaryWithObjectsAndKeys:
                                          @"Scisser",@"name",
                                          @"http://m.facebook.com/apps/uniquenamespace/",@"link",
                                          nil],
                                         nil];
    
                 NSString *actionLinksStr = [actionLinks JSONRepresentation];
    
                 if (strLink.length == 0)
                 {
                     strLinkBlock = @"http://www.scisser.com";
                 }
    
                 NSString *strDescription = strDesc;
                 if (!strDescription)
                 {
                     strDescription = @"";
                 }
    
                 NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                strName, @"name",
                                                strCaption, @"caption",
                                                strDescription, @"description",
                                                strLinkBlock, @"link",
                                                strPicLink, @"picture",
                                                @"foo", @"ref",
                                                actionLinksStr, @"actions",
                                                nil];
    
                 // Make the request
                 [FBRequestConnection startWithGraphPath:@"/me/feed"
                                              parameters:params
                                              HTTPMethod:@"POST"
                                       completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                           if (!error)
                                           {
                                               // Link posted successfully to Facebook
    
                                               [self alertshowWithTitle:@"Congratulations" message:@"Post was successfully shared on your Facebook timeline."];
    
                                               NSLog(@"%@",[NSString stringWithFormat:@"result: %@", result]);
    
                                               completionBlock(YES);
                                               return;
                                           }
                                           else
                                           {
                                               // An error occurred, we need to handle the error
                                               // See: https://developers.facebook.com/docs/ios/errors
                                               NSLog(@"%@",[NSString stringWithFormat:@"%@", error.description]);
    
                                               completionBlock(NO);
                                               return;
                                           }
                                       }];
    
             }
         }];
    }
    

    【讨论】:

    • 什么是 ShareCompletion 类型?
    • 我怎样才能像 instagram 一样将我的 facebook 照片重定向到我的应用程序。例如,当您将照片从 instagram 分享到 facebook 时。然后在 facebook 上的同一个共享帖子上,您选择“在 instagra 中打开”,它会将您带到 instagram 上的同一个帖子。我怎样才能做到这一点?
    【解决方案2】:

    为了启用与 FB 的深度链接,您需要通过 FB 开发者门户https://developers.facebook.com 在您的 FB 应用设置中注册您的 iOS 应用 ID/捆绑标识符。完成后,您需要在 Xcode 的 Info.plist 文件中为您的应用程序注册一个自定义 URL 方案。自定义 URL 方案应该是您的 FB 应用程序 ID,前缀为 fb - 因此,如果您的 FB 应用程序 ID 是 12345,您的自定义 URL 方案将是 fb12345。一旦完成并且您的应用程序启动并提交到 App Store,只需使用您通常用于 MY_URL 的常规 http:// URL - 此 URL 将作为 target_url 参数传递。您可以通过 AppDelegate 的

     - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    

    当您的应用使用您的自定义 URL 方案启动时将调用的方法。然后你用这样的东西解析它:

    NSRange targetURLRange = [urlAsString rangeOfString:@"target_url="];
    
    if (targetURLRange.location != NSNotFound)
    {
        NSString *FBTargetURLAsString = [urlAsString substringFromIndex:(targetURLRange.location + targetURLRange.length)];
        DDLogVerbose(@"After cutting I got: %@", FBTargetURLAsString);
    
        FBTargetURLAsString = [FBTargetURLAsString stringByReplacingPercentEscapesUsingEncoding:NSStringEncodingConversionAllowLossy];
    
        DDLogVerbose(@"After processing I got: %@", FBTargetURLAsString);
    
        NSArray *brokenDownURL = [FBTargetURLAsString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/?&="]];
        DDLogVerbose(@"Broken down URL: %@", brokenDownURL);
    }
    

    您检查的实际参数取决于您。例如,我们使用路径.../ogfb/object_type/object_id,所以我会使用:

        NSString *objectType;
        NSString *objectID;
    
        for (NSString *currentURLComponent in brokenDownURL)
        {
            if ([currentURLComponent caseInsensitiveCompare:@"ogfb"] == 0)
            {
                NSInteger ogfbIndex = [brokenDownURL indexOfObject:currentURLComponent];
    
                if ([brokenDownURL count] > ogfbIndex + 2)
                {
                    objectType = [brokenDownURL objectAtIndex:ogfbIndex + 1];
                    objectID = [brokenDownURL objectAtIndex:ogfbIndex + 2];
                }
    
            }
        }
    
        DDLogVerbose(@"Got object type: %@ with ID: %@", objectType, objectID);
    

    然后你做你需要用它做的事情,应该很好!是的,这将要求您将您的应用重新提交到 App Store(以注册新的自定义 URL 方案)。带有自定义 URL 方案的 Info.plist 部分在 XML 中如下所示(如下)。关键路径是URL Types(数组) -> 项目0 -> URL Schemes(数组) -> 项目0 -> fb<yourFBAppIDhere>

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb(yourFBAppIDhere)</string>
            </array>
        </dict>
    </array>
    </plist>
    

    【讨论】: