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

iOS(React Native)中如何排除根路径仅启用指定Universal Link

仅让指定路径触发iOS Universal Link的解决方案

问题概述

我在为React Native应用测试iOS Universal Link,需求是仅https://<random-domain>.ngrok-free.app/launch.html路径能唤起APP,根路径及其他子路径(如/test)均不触发,但目前域名下所有路径都能打开APP。

问题根源

1. AASA文件路径规则错误

当前AASA的多组details配置和错误的匹配模式导致规则逻辑混乱,苹果会优先匹配允许的规则,最终所有路径都被放行:

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "ABC123.com.test.Test",
        "paths": ["/launch/*", "NOT *"]
      },
      {
        "appID": "ABC123.com.test.Test",
        "paths": ["NOT /", "NOT *"]
      }
    ]
  }
}

2. Express服务器存在两处问题

  • 变量名错误:response未定义,应该使用res
  • 未设置正确的Content-Type,苹果要求AASA文件必须以application/json类型返回,否则可能解析失败
const express = require("express");
const app = express();
const port = 3000;

app.get("/.well-known/apple-app-site-association", (req, res) => {
  response.sendFile(__dirname + "/apple-app-site-association"); // 变量名错误
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

修复步骤

1. 修正AASA文件

苹果的路径匹配是从上到下匹配,命中即停止,只需精确指定允许的路径,再排除所有其他路径即可:

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "ABC123.com.test.Test",
        "paths": [
          "/launch.html", // 精确匹配目标路径
          "NOT *"         // 排除所有其他路径
        ]
      }
    ]
  }
}

2. 修复Express服务器代码

修正变量名并设置正确的Content-Type:

const express = require("express");
const app = express();
const port = 3000;

app.get("/.well-known/apple-app-site-association", (req, res) => {
  res.setHeader("Content-Type", "application/json");
  res.sendFile(__dirname + "/apple-app-site-association");
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

3. 生效验证

  • 重启Express服务器,重新通过ngrok暴露端口
  • 重置iOS设备的Universal Link缓存:删除APP后重新安装,或在模拟器中执行Debug -> Reset Content and Settings
  • 测试各路径:
    • https://<random-domain>.ngrok-free.app:浏览器打开,不唤起APP
    • https://<random-domain>.ngrok-free.app/test:浏览器打开,不唤起APP
    • https://<random-domain>.ngrok-free.app/launch.html:唤起APP

4. 权限配置无需修改

现有associated-domains配置正确,保持原样即可:

<?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">
<dict>
    <key>com.apple.developer.associated-domains</key>
    <array>
        <string>applinks:<random-domain>.ngrok-free.app</string>
        <string>applinks:<random-domain>.ngrok-free.app?mode=developers</string>
    </array>
</dict>
</plist>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 08:29:57