适用于 IOS 和 Android 应用程序的 Deeplink 解决方案适用于 Facebook

2024-03-14

深度链接(通用链接或应用程序链接)教程太多了。但大多数都展示了如何在 Android 或 IOS 应用程序中启用它。还有付费云解决方案,但它们提供了很多功能。但我在现实生活中面临三个主要问题:

  1. 某些浏览器不允许应用程序链接工作。例如,您可以将 http://example.com 配置为在应用程序中捕获,但如果用户通过 Facebook 应用程序单击此链接,则不会处理该链接,并且 Facebook 浏览器会显示该网站。
  2. 没有唯一的标准解决方案来处理 Android 和 IOS 应用程序的链接。
  3. 如果移动设备上未安装应用程序并且用户单击应用程序链接,则没有实际的解决方案。

我写了这个问答,这是我研究(花了太多时间)的结果,以获得一个独特且适用于所有案例的解决方案。

这些代码来自我的工作解决方案,但我删除了一些部分只是为了展示这个想法。如果编译出现问题,请按照算法编写自己的代码

这是解决方案,即使你知道一些步骤,也要一步一步来,因为代码中有技巧。代码部分的注释行中也有一些解释,请阅读它们。

示例是通过 Android 和 IOS 应用程序处理深度链接 http://example.com/v/,并在末尾添加参数,例如 http://example.com/v/id-of-user?key=value。

1. 配置安卓

1.1 将活动信息添加到 AndroidManifest.xml 文件中:

<activity
android:name=".appLinkHandlerActivity">

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data
        android:host="example.com"
        android:pathPrefix="/v/"
        android:scheme="http" />
</intent-filter>

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

<!—this intent is needed to handle links to myapp://share, I will explain later why we need it -->
    <data
        android:host="share"
        android:scheme="myapp" />
</intent-filter>

1.2 创建一个名为 appLinkHandlerActivity 的活动,它将处理单击的链接

    public class appLinkHandlerActivity extends AppCompatActivity {


    /* assume that user is clicked http://example.com/v/my-user-id   
    actCode will be “v”, pCode will be “my-user-id” */
    String actCode="", pCode="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // ATTENTION: This was auto-generated to handle app links.
        Intent appLinkIntent = getIntent();
        String appLinkAction = appLinkIntent.getAction();
        Uri appLinkData = appLinkIntent.getData();



        String code = null;
        try {
            code = getIntent().getData().getLastPathSegment();
        } catch (Exception e) {

        }

        if (code == null) {
            Intent i = new Intent(this, {your main activity.class});
            startActivity(i);
        }

        List<String> params=appLinkData.getPathSegments();


        if (params.size()>0)
            actCode=params.get(0);

        if (params.size()>=2)
            pCode=params.get(1);

        /* assume that user is clicked http://example.com/v/my-user-id actCode is “v”, pCode is “my-user-id”  Do now whatever you need. */
    } 
 }

2.配置IOS

这比Android更复杂,我将在这里解释必要的要点。请参考文档:https://developer.apple.com/library/content/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12-SW1 https://developer.apple.com/library/content/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12-SW1

https://www.raywenderlich.com/128948/universal-links-make-connection https://www.raywenderlich.com/128948/universal-links-make-connection

2.1 在 Apple Developer Portal 上创建 App ID 时,您必须启用关联域。重要问题:您需要拥有购买的 Apple 开发者帐户才能启用此选项,这意味着如果不购买开发者帐户,您将无法在 IOS 项目上尝试 AppLinks。

2.2 在XCode项目中,打开“Capabilites”选项卡并将Associated Domains切换为On。如果您没有在 Apple Developer Portal App ID 部分启用关联域,则可能无法选择 单击关联域选项下的 + 按钮添加权利,写入“applinks:example.com”。

2.3 在您的 Web 服务器上创建一个名为“apple-app-site-association”的文件,并且必须通过 https://example.com/apple-app-site-association 访问此文件 如果 HTTPS 不是有效的,则 HTTPS 是强制的SSL 证书应用程序链接可能不起作用。将以下行添加到 apple-app-site-association 文件中:

{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "6HY7TF56.com.example.app",
                "paths": [ "/ios/*", "/v/*" ]
            }
        ]
    }
}

appID 的格式为{“Team ID”.“Bundle ID of your App”}。您可以在开发者门户的“会员详细信息”部分找到您的 teamID。

我们处理链接 http://example.com/v/parameters,但在这里您会看到“/ios/*”有另一个路径配置。需要绕过不支持的浏览器,我稍后会解释。

2.4 在 AppDelegate.m 文件中添加两个方法来处理用户对 example.com 的点击

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
    if ([userActivity.activityType isEqualToString: NSUserActivityTypeBrowsingWeb]) {

        NSURL *url = userActivity.webpageURL;
          [self parseUrl:url];

    }
return YES;
}


- (void) parseUrl:(NSURL * )handledUrl {
    NSString *urlStr=@"";
    NSString *pCode=@"";
    NSString *handledUrlStr=[handledUrl parameterString];

    NSString *handledUrlQueryPart;

    NSArray<NSString *> *pathsArray= [handledUrl pathComponents];

    //remember that we only added paths “/v/*” and “/ios/*” to handle in apple-app-site-association file. If you want to handle more subpaths you can add them into apple-app-site-association file, then below if-else conditions. Don’t touch to config and code for “/ios/*” path, it is needed to bypass unsopported browsers.
    if ([pathsArray[1]  isEqual: @"v"]){
        //sample url= http://example.com/v/menu?aaa=bbb
        pCode = pathsArray[2];
        handledUrlQueryPart=[handledUrl query];
    } else if ([pathsArray[1]  isEqual: @"ios"]){
        //sample url= http://example.com/ios/deeplink-ios.php?/v/menu?aaa=bbb
        NSArray *uriArray = [[handledUrl query] componentsSeparatedByString:@"?"];
           NSArray *queryPathsArray = [uriArray[0] componentsSeparatedByString:@"/"];
        if ([queryPathsArray count] > 2)
            pCode = queryPathsArray[2];

        if ([uriArray count] > 1 ){
            handledUrlQueryPart=uriArray[1];
        }
    }

    /* here pCode is the parameter what is passed from user. If the link clicked is http://example.com/v/menu it is “menu”. If the link clicked is http://example.com/v/menu?aaa=bbb it is “menu?aaa=bbb”. So you can do now what ever you need. */    
}

3. 管理未捕获的点击。

3.1 好的,您的 Android 和 IOS 应用程序应该处理链接 http://example.com/v/blabla 的点击,并将“blabla”参数传递给我展示的方法中使用的 pCode 变量。但某些浏览器(例如 Facebook 应用程序)可能会禁用应用程序链接。在这种情况下,用户单击将转到您的 Web 服务器,浏览器会尝试显示 http://example.com/v/blabla 的内容,这可能是 404 Page Not Found。为了处理这些点击,我们将配置 Apache Web 服务器并将用户重定向到您的应用程序。如果你使用 IIS 或其他,我不知道该怎么做,但你可以以此为示例并使用相同的算法来配置你的 Web 服务器。

3.2 在 example.com 根目录下的 .htaccess 文件中写入以下行

#redirect to deeplink
<IfModule mod_rewrite.c>

#if there is a request to example.com/v/any-sub-path, redirect them to example.com/deeplink.php file. This rule is for both IOS and Android
RewriteRule ^(v)/.* /deeplink.php [L]

#if there is a request to example.com/ios/any-sub-path, redirect them to app installation page. That means your app is not installed on IOS device. This rule is for IOS devices only
RewriteRule ^(ios)/.* http://itunes.apple.com/install-of-your-app [L] 
</IfModule>  

4. 将用户重定向到应用程序

4.1 步骤3中显示的.htaccess文件中的重定向规则将用户重定向到deeplink.php文件。因此,这是该文件的内容,用于将用户重定向到您的应用程序。

 <?php

$request_uri=$_SERVER[REQUEST_URI];

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') == true){
//if user device is android, redirect it to intent url which is handled by Android.
        $redir_tag="<meta http-equiv='refresh' content='0;url=intent://share$request_uri/#Intent;scheme=myapp;S.browser_fallback_url=http%3A%2F%2Fexample.com%2Fget-app%2F;package=com.example.app;end' />";
//scheme=myapp and host named “share” is configured in AndroidManifest.xml file to be handled by the activity.
//fallback url is the url, if your app is not installed on android device, so you can redirect them to a page to install android app. In some cases users are redirected to Play Store directly for application id of com.example.app
}

else if ( (stripos($ua,'iPhone') == true) || (stripos($ua,'iPad') == true) ) {
        //if user device is IOS, redirect them to a web page to see. There will be a link here to the another handled link: http://example.com/ios/blabla.
        // due to my experience there is no way to redirect IOS to app directly at this stage, user must click a link on browser and that link must be different than the link which was shown and clicked at first.
        // another experience taught me ther ecan be problems if we redirect users to a web page under example.com which is configured as applink, so I redirect users to a page under deep.example.com here
        $redir_tag="<meta http-equiv='refresh' content='0;url=http://deep.example.com/deeplink-ios.php?$request_uri' />";
}

else {
//If the device is neither IOS nor Android, redirect users to a web page which gives information that this link is for Android and IOS only
        $redir_tag="<meta http-equiv='refresh' content='0;url=http://example.com/non-mobile' />";
}


?>



<html>
    <head>

        <!— add tags for no-caching, this is important, the date below is my son’s birth time and date, he is now 6, so you can use it as a date in the past -->
        <meta http-equiv="cache-control" content="max-age=0" />
        <meta http-equiv="cache-control" content="no-cache" />
        <meta http-equiv="expires" content="-1" />
        <meta http-equiv="expires" content="Tue, 31 May 2011 10:15:00 GMT+3" />
        <meta http-equiv="pragma" content="no-cache" />


        <?php echo $redir_tag; ?>

    </head>
</html>

5.向IOS用户显示一个可以点击的链接

5.1 这是http://deep.example.com/deeplink-ios.php 文件的内容。用户将看到如下页面,当他们点击链接时,该请求应该由您的 IOS 应用程序处理。

<?php
//we create a link to http://example.com/ios/… which is configure to be handled by IOS app. IOS needs to be a user click in some cases to handle the request, that is why this page is shown to the user
$request_uri=$_SERVER[REQUEST_URI];
$link="<div class='w3-container w3-card'><h1><a href='http://example.com/ios$request_uri'>Click here to open MyApp</a></h1></div>";
?>

<html>
    <head>

        <!—adding no-cache tags is also important here-->
        <meta http-equiv="cache-control" content="max-age=0" />
        <meta http-equiv="cache-control" content="no-cache" />
        <meta http-equiv="expires" content="-1" />
        <meta http-equiv="expires" content="Tue, 31 May 2011 10:15:00 GMT+3" />
        <meta http-equiv="pragma" content="no-cache" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3mobile.css">

    </head>
<body>

<?php echo $link ?>

</body>
</html>

六、案例分析:

6.1 安卓

6.1.1 案例1 – 应用程序安装在设备上
• 浏览器请求http://example.com/v/blabla
• Android 捕获链接并创建在清单文件中配置的活动

6.1.2 案例2 - 应用程序安装在设备上
• 浏览器请求http://example.com/v/blabla
• Android 无法捕获链接。
• 浏览器连接到 Web 服务器,请求 /v/blabla
• 由于.htaccess 文件中的配置,它被重定向到deeplink.php?/v/blabla
• deeplink.php 得知它是android,并重定向到:intent://share/v/blabla/#Intent;scheme=myapp;S.browser_fallback_url=http%3A%2F%2Fexample.com%2Fget-app%2F;包=com.example.app
• Android 捕获针对 Intent:// 的请求,因此由于清单文件 myapp://share/v/blabla 中的配置,由 Activity 处理

6.1.3 情况3-应用程序未安装
• 浏览器请求http://example.com/v/blabla
• Android 无法捕获链接。
• 浏览器连接到 Web 服务器,请求 /v/blabla
• 由于.htaccess 文件中的配置,它被重定向到deeplink.php?/v/blabla
• deeplink.php 得知它是android,并重定向到:intent://share/v/blabla/#Intent;scheme=myapp;S.browser_fallback_url=http%3A%2F%2Fexample.com%2Fget-app%2F;包=com.example.app
• Android 捕获针对intent:// 的请求,但没有为id: com.example.app 安装应用程序。在某些情况下,它会退回并将浏览器重定向到 http://example.com/get-app 或应用程序的 Play 商店安装页面

6.2 IOS

6.2.1 案例1 – 应用程序安装在设备上
• 浏览器请求http://example.com/v/blabla
• IOS 捕获链接并调用 AppDelegate.m 中的 continueUserActivity 方法

6.2.2 案例2 – 应用程序安装在设备上
• 浏览器请求http://example.com/v/blabla
• IOS 无法捕获链接。
• 浏览器连接到 Web 服务器,请求 /v/blabla
• 由于.htaccess 文件中的配置,它被重定向到deeplink.php?/v/blabla
• deeplink.php 得知它是 IOS,并重定向到:http://deep.example.com/deeplink-ios.php?/v/blabla
• deeplink-ios.php 文件向用户显示URL。网址是:http://lify.me/ios/v/blabla
• 用户单击 URL,浏览器请求 http://lify.me/ios/v/blabla
• IOS 根据 apple-app-site-association 文件中路径“/ios/*”的配置捕获请求,并调用 AppDelegate.m 中的 continueUserActivity 方法
• 如果 IOS 由于任何原因无法捕获 http://lify.me/ios/v/blabla 的请求,则会表现为设备上未安装应用程序。看看那个案例。

6.2.3 案例2 – 设备上未安装应用程序
• 浏览器请求http://example.com/v/blabla
• IOS 无法捕获链接。
• 浏览器连接到 Web 服务器,请求 /v/blabla
• 由于.htaccess 文件中的配置,它被重定向到deeplink.php?/v/blabla
• deeplink.php 得知它是 IOS,并重定向到:http://deep.example.com/deeplink-ios.php?/v/blabla
• deeplink-ios.php 文件向用户显示URL。网址是:http://lify.me/ios/v/blabla
• 用户单击 URL,浏览器请求 http://lify.me/ios/v/blabla
• 如果 IOS 无法捕获 http://lify.me/ios/v/blabla 的请求
• 浏览器连接到 Web 服务器,请求 /ios/v/blabla
• 由于 Web 服务器上 .htaccess 文件中的配置,它被重定向到 http://itunes.apple.com/install-of-your-app

6.3 非Android或IOS设备上点击App Link
• 浏览器请求http://example.com/v/blabla
• 设备操作系统无法捕获链接。
• 浏览器连接到 Web 服务器,请求 /v/blabla
• 由于.htaccess 文件中的配置,它被重定向到deeplink.php?/v/blabla
• deeplink.php 得知它既不是 IOS 也不是 Android,并重定向到:http://example.com/non-mobile


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

适用于 IOS 和 Android 应用程序的 Deeplink 解决方案适用于 Facebook 的相关文章

  • FBSessionDelegate 方法未触发

    我正在尝试实施最新的 Facebook Connect SDK 但遇到了一些麻烦 由于某种原因 委托回调FBSessionDelegate协议没有被解雇 我已按照 git Facebook 页面上的说明进行操作 并尝试模仿 Facebook
  • { [FacebookTokenError:此授权码已被使用。]

    我有一个航行应用程序 我正在尝试实现 Facebook 登录 当我单击 使用 facebook 登录 按钮时 出现此错误 error A server error occurred in a request error FacebookTo
  • 如何等待在react.js中渲染视图直到$.get()完成?

    您好 我正在用 Reactjs 编写一个聊天客户端 并希望使用从 REST 调用检索到的数据来渲染我的组件 但是 我的组件是在 REST 请求返回数据之前渲染的 当我在我的子组件中调用 this props 时 这会导致错误 var Mai
  • CakePHP Facebook 集成与 CakePHP-Facebook-Plugin 的注销问题

    我正在寻找一种方法CakePHP Facebook 插件 https github com webtechnick CakePHP Facebook Plugin让用户退出我的应用程序 但不让他们退出他们自己的 Facebook 如果我调用
  • Facebook Analytics:使用图形 API 的用户属性和记录事件

    我有 facebook 应用程序 Messenger Bot 应用程序 我使用图形 API 端点记录每个用户的自定义事件 申请 活动 事件被完美记录 我想要做的是为我的信使机器人用户创建自定义属性 以便我可以使用此属性对应用程序数据进行分段
  • Facebook 点赞按钮点赞错误的网址

    如果我尝试制作一个 facebook like 按钮来喜欢以下网址 http go style co uk nggallery page 7029 image 35 该按钮喜欢以下网址 http go style co uk test ga
  • Facebook python sdk使用营销api或图api

    我一直在使用facebook python SDK花了近 6 个月的时间来构建一个产品 我拥有营销 API 的开发访问权限 它经常错误地指出已超出速率限制 但是当我检查营销 API 仪表板时 它说没有足够的可用数据 所以我真的很困惑 SDK
  • 使用 php 在没有“manage_pages”权限的情况下发布到 Facebook 页面

    我有一个包含博客文章的网站 我们需要自动将博客发布到 Facebook 页面 目前我可以发布到我的时间线 但我无法发布到 Facebook 页面 我在谷歌搜索过 许多代码说我们需要manage pages权限 我的应用程序 Facebook
  • 在 Android 中使用 Facebook Achievement API

    我知道这可能看起来像一个通用问题 但找到有关该主题的信息似乎非常困难 因此 如果某个地方存在完整的示例 指南 源代码链接 我将不胜感激 我正在开发一款 Android 游戏 希望集成 Facebook 成就 我想要的只是在用户完成某个谜题时
  • 何时应使用服务器端与客户端 Facebook 身份验证流程?

    Facebook 有两个身份验证流程 客户端和服务器端 每一项应该在什么时候使用 脸书文档 https developers facebook com docs authentication https developers faceboo
  • 在 Facebook 上分享链接。 Facebook 不获取描述和图像

    我正在开发sucsongmoi net 越南语 当浏览者从他们的墙上分享网站链接时 某些链接 facebook 会获取描述和图像 某些链接 facebook 无法获取描述和图像 例如 分享sucsognmoi netfacebook 没有得
  • 元标记内的属性 property="og:title" 是什么?

    我有网站源代码的摘录 这是做什么的属性属性代表什么 其目的是什么 og title是开放图元标签之一 og 属性定义社交图中的对象 例如 Facebook 使用它们 og title代表对象的标题 因为它应该出现在图表中 请参阅此处了解更多
  • FB.getLoginStatus() 不起作用

    我正在尝试编写一段代码来检查用户是否登录 发现FBJS API中有一个内置方法 叫做getLoginStatus 我已经在html中实现了它 但出于某种原因 getLoginStatus 内部的alert 不会被触发 我也尝试在 init
  • 使用 PassportJS 和 Connect for NodeJS 对 Facebook 用户进行身份验证

    我正在尝试使用 connect 将 Passport 集成到我的 NodeJS 服务器中 但似乎无法正确执行 所有指南 示例都使用expressJS 因此我尽力重新格式化代码以与我的代码一起使用 但我似乎无法让它工作 相关部分写在下面 有人
  • 为什么 FB.Event.subscribe 会触发?

    是什么导致 FB Event subscribe 事件触发 我知道单击 fb login button 元素可以触发它 但只要创建 Facebook 会话 FB Event subscribe 事件就会触发 当存在 Facebook 会话时
  • 使用 Graph API 删除 facebook 帖子 - 无法正常工作

    我使用以下命令在我的 Facebook 页面上发布消息 attachment array access token gt access token message gt This is a test Message 4 name gt Th
  • Android 上的 Facebook 深度链接

    我正在尝试在我的应用程序上实现 Facebook 的深度链接功能 并遇到了以下情况 我有一个名为 MainActivity 的活动 其声明如下
  • 是否可以使用 facebook oauth 2.0 身份验证创建桌面应用程序而不需要浏览器交互?

    是否有可能拥有一个可以使用 Facebook 进行身份验证但不需要浏览器的客户端应用程序 我的意思是代码中没有嵌入浏览器 用户输入用户名 密码并仅允许使用客户端应用程序访问应用程序 任何建议都会有很大帮助 谢谢 塔拉 辛格 编辑 我已经用
  • 我在 Facebook 上发现的这个奇怪的脚本是什么?

    这并不是一个帮助我自己编程的问题 但我在 Facebook 上发现了这个页面 其中有一个很酷的幻觉 并且一个页面上写着 要看到真正的幻觉 请将此代码复制并粘贴到您的地址栏中 并且有一个脚本 免责声明 请勿运行以下代码 javascript
  • ShareKit + Facebook 身份验证不起作用

    我正在使用 ShareKit 从我正在开发的 PhoneGap 应用程序发布到 Facebook 我在 Facebook 中创建了该应用程序 并在我的项目中安装了该插件 Twitter 工作正常 但当我尝试在 Facebook 上分享时出现

随机推荐