且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-01 17:48:13

你实际上在这个流程中使用了两个不同的Facebook工具:

You're actually making use of two different Facebook tools with this flow:


  1. 你要分享的应用程序内的FBSDK一个链接(看起来你可能从这里获得了你的代码示例? )

  2. Facebook App链接在链接打开时启动你的应用程序。

  1. The FBSDK inside your app to share a link (looks like you might have got your code example from here?)
  2. Facebook App Links to launch your app when the link is opened.

这个过程Facebook对App Links的使用实际上有点复杂......不幸的是,它并不像添加URL参数那么简单。以下是其工作原理的概述(来源):

The process Facebook uses for App Links is actually a bit more complicated...unfortunately it's not quite as simple as just appending a URL parameter. Here's an overview of how it works (source):


  1. 您发布了指向您内容的链接(您正在使用FBSDK)

  2. 链接指向主机页面一些特殊的App Links元标记。其中一个看起来像< meta property =al:ios:urlcontent =yourapp:// path / to / content/> 当Facebook抓取时链接并看到这些标记,它将链接标记为已启用App链接。您可以将这些标签添加到您自己的网站,如果您有一个链接指向它,或Facebook提供移动主机API (如果您没有网站,则显示您正在使用)。

  3. 当用户打开此链接时,你的应用程序会启动,你将会传递一个这样的网址,你的应用必须为自己处理: yourapp:// path / to / content?al_applink_data = JSON_ENCODED_DATA 。遗憾地,重定向到应用程序中的正确位置并不神奇,并假设您已将应用程序设置为基于URL方案结构显示内容。 Facebook 提供了演练,但基本上你需要在你的AppDelegate.m中添加这样的东西。 :

  1. You post a link to your content (you're using the FBSDK for this)
  2. The link points at a page that hosts some special App Links meta tags. One of these will look like <meta property="al:ios:url" content="yourapp://path/to/content" /> When Facebook crawls the link and sees these tags, it flags the link as enabled for App Links. You can add these tags to your own site, if you have one and the link points to it, or the Facebook offers the Mobile Hosting API (which it appears you're using) if you don't have a website.
  3. When a user opens this link, your app launches and you'll be passed a URL like this, which your app has to process for itself: yourapp://path/to/content?al_applink_data=JSON_ENCODED_DATA. The redirection to the correct location in your app sadly isn't magic, and assumes you've set up your app to display content based on a URL scheme structure. Facebook offers a walkthrough, but basically you need to add something like this to your 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];
    }
    ...
}


这只包括你通过Facebook共享,并没有陷入URL方案深层链接或苹果公司在iOS 9中的新通用链接的混乱。如果你想处理所有这些,除了Facebook App Links,您还可以考虑免费服务,例如 Branch.io (完全披露:它们真是太棒了我与他们合作)为您处理所有技术细节。

This only covers you for sharing through Facebook though, and doesn't get into the mess that is URL scheme deep links or Apple's new Universal Links in iOS 9. If you want to handle all of those, in addition to Facebook App Links, you could consider a free service like Branch.io (full disclosure: they're so awesome I work with them) to take care of all the technical details for you.