你如何使用ios中的facebook分享按钮的应用程序链接进行深层链接?

时间:2016-03-08 09:01:50

标签: ios facebook deep-linking applinks

我正在尝试使用fb分享按钮从我的应用创建Facebook的帖子。我想要使​​用applinks将此帖子链接回应用。我不明白如何将数据添加到facebook网址,以便知道在点击链接时将应用程序指向何处。

FBSDKShareLinkContent * content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:@"https://fb.me/123456789"];
FBSDKShareButton * button = [[FBSDKShareButton alloc] init];
button.shareContent = content;
[self.view addSubview:button];

这样可行,但我尝试添加到fb url的任何查询都会中断并且不再链接到该应用。例如:

 content.contentURL = [NSURL URLWithString:@"https://fb.me/123456789?my_view=news"];

这是他错误的做法吗?我也尝试使用json编码和al_applink_data,但对原始网址的任何更改都会使帖子链接到图片网址,而不是应用链接。

1 个答案:

答案 0 :(得分:2)

您实际上正在使用两种不同的Facebook工具:

  1. 您的应用内的FBSDK共享链接(看起来您可能已经从here获得了代码示例?)
  2. Facebook App链接以在链接打开时启动您的应用程序。
  3. Facebook用于App Links的过程实际上有点复杂......不幸的是,它并不像添加URL参数那么简单。以下是对其工作原理的概述(source):

    1. 您发布了指向您内容的链接(您为此使用了FBSDK)
    2. 链接指向托管一些特殊App Links元标记的页面。其中一个看起来像<meta property="al:ios:url" content="yourapp://path/to/content" />当Facebook抓取链接并看到这些标记时,它会将链接标记为已启用App链接。您可以将这些标签添加到您自己的网站,如果您有一个链接指向它,或者Facebook提供Mobile Hosting API(您似乎正在使用它),如果您没有一个网站。
    3. 当用户打开此链接时,您的应用就会启动,并且您将传递一个这样的网址,您的应用必须自行处理 yourapp://path/to/content?al_applink_data=JSON_ENCODED_DATA 。重定向到您应用中的正确位置可能并不神奇,并假设您已设置应用以基于URL方案结构显示内容。 Facebook offers a walkthrough,但基本上你需要在你的AppDelegate.m中添加这样的东西:

      - (BOOL)application:(UIApplication *)application
                  openURL:(NSURL *)url
        sourceApplication:(NSString *)sourceApplication
               annotation:(id)annotation {
          BFURL *parsedUrl = [BFURL URLWithInboundURL:url sourceApplication:sourceApplication];
          if ([parsedUrl appLinkData]) {
              // this is an applink url, handle it here
              NSURL *targetUrl = [parsedUrl targetURL];
              [[[UIAlertView alloc] initWithTitle:@"Received link:"
                                          message:[targetUrl absoluteString]
                                         delegate:nil
                                cancelButtonTitle:@"OK"
                                otherButtonTitles:nil] show];
          }
          ...
      }
      
    4. 这仅涵盖您通过Facebook共享,并且不会陷入URL方案深层链接或Apple在iOS 9中的新通用链接的混乱中。如果您想处理所有这些除了Facebook App Links之外,您还可以考虑免费服务,例如Branch.io(完全披露:他们与我们合作非常棒),为您处理所有技术细节。