You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用中实现浏览器链接唤起App打开指定页面的方法

我来帮你搞定这个唤起WebView App的需求!要让用户访问www.website.com/offer.php?root=yes时自动唤起你的原生App,需要分别针对Android、iOS平台做配置,再配合网页端的兜底处理,下面一步步拆解:

一、Android 端配置

1. 给WebView页面添加Intent Filter

在你的App的AndroidManifest.xml文件中,找到承载WebView的Activity,添加Intent Filter来匹配目标链接:

<activity android:name=".YourWebViewActivity">
    <!-- 原有配置保留 -->
    <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:scheme="https"
            android:host="www.website.com"
            android:pathPrefix="/offer.php"
            android:query="root=yes" />
    </intent-filter>
</activity>

说明:scheme用https是因为你的链接是HTTPS协议,host指定域名,pathPrefix匹配路径,query精准锁定root=yes参数,这样系统遇到符合条件的链接时,会弹出选择框让用户打开你的App。

2. 在Activity中处理传入的链接

当App被唤起时,需要获取浏览器传递的链接,加载到WebView中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    
    WebView yourWebView = findViewById(R.id.webview);
    // 初始化WebView配置(略)
    
    // 获取唤起链接
    Uri data = getIntent().getData();
    if (data != null) {
        String targetUrl = data.toString();
        yourWebView.loadUrl(targetUrl);
    }
}

如果App已经在后台运行,还需要在onNewIntent方法中做同样的处理,避免链接不更新。

二、iOS 端配置

iOS推荐用Universal Links(通用链接)来实现,它是苹果官方推荐的HTTPS唤起方式,体验更顺滑。

1. 配置Associated Domains

  • 登录苹果开发者后台,找到你的App ID,开启Associated Domains权限。
  • 在Xcode中,打开你的项目,进入Signing & Capabilities,添加Associated Domains,并填入applinks:www.website.com。

2. 部署Apple App Site Association文件

在你的网站根目录或.well-known目录下,创建一个名为apple-app-site-association的文件(无后缀),内容如下:

{
    "applinks": {
        "details": [
            {
                "appIDs": ["你的TeamID.你的BundleID"],
                "components": [
                    {
                        "/": "/offer.php",
                        "?": { "root": "yes" }
                    }
                ]
            }
        ]
    }
}

注意:

  • 你的TeamID是苹果开发者账号的团队ID,你的BundleID是App的唯一标识,两者都可以在开发者后台找到。
  • 这个文件必须通过HTTPS访问,不能有重定向,MIME类型需设置为application/json。

在SceneDelegate(或AppDelegate,取决于你的项目架构)中添加处理逻辑:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
          let targetUrl = userActivity.webpageURL else {
        return
    }
    // 将链接加载到WebView
    if let webViewVC = window?.rootViewController as? WebViewController {
        webViewVC.webView.load(URLRequest(url: targetUrl))
    }
}
三、网页端兜底处理(可选但推荐)

部分浏览器可能不会自动触发App唤起,这时候可以用JavaScript做一层兜底,尝试唤起App,失败则留在网页:

window.addEventListener('load', () => {
    const targetUrl = window.location.href;
    let fallbackTimer = setTimeout(() => {
        // 1秒后没唤起成功,留在当前页面
        window.location.href = targetUrl;
    }, 1000);

    // 通过iframe尝试唤起App(避免页面跳转)
    const iframe = document.createElement('iframe');
    iframe.style.display = 'none';
    iframe.src = targetUrl;
    document.body.appendChild(iframe);

    // 若App唤起成功,页面会失去焦点,清除定时器
    window.addEventListener('blur', () => {
        clearTimeout(fallbackTimer);
        document.body.removeChild(iframe);
    });
});
四、测试小技巧
  • Android:用adb命令快速测试:adb shell am start -a android.intent.action.VIEW -d "https://www.website.com/offer.php?root=yes",看是否唤起你的App。
  • iOS:必须在真机上测试(模拟器不支持Universal Links),用Safari打开目标链接,看是否弹出“打开App”的提示。
  • 验证Universal Links:可以用苹果官方的验证工具确认配置是否正确。

内容的提问来源于stack exchange,提问作者Swarn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:52:12