繁体   English   中英

Facebook与iOS应用程序的深层链接

[英]Facebook deep linking with iOS app

我使用下面的代码发布到Facebook。 它工作得很好但是当我用myapp:// test_page替换MY_URL//帖子不会出现在Facebook时间线上。

如果我做错了,那么请告诉我如何将我的应用程序深入链接到Facebook应用程序。 我搜索了几乎所有的stackoverflow.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");
 }];

谢谢你回答@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;
                                       }
                                   }];

         }
     }];
}

要启用与FB的深层链接,您需要通过FB开发人员门户https://developers.facebook.com在FB应用程序的设置中注册您的iOS应用程序ID /包标识符。 完成后,您需要在Info.plist文件中的Xcode中为您的应用注册自定义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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM