You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android DeepLink使用求助:点击链接仅打开Chrome无法唤起应用

修复自定义DeepLink无法唤起应用的问题

看起来你的自定义DeepLink sosapp://sosapp/hashcode/ghfhgfhgf? 没有被系统正确识别,导致点击时直接跳转Chrome。我们分Android和iOS两个平台来一步步排查修复:


Android 端配置排查

1. 检查 AndroidManifest.xml 的 Intent Filter 配置

你需要在要唤起的Activity(比如主Activity)中添加正确的<intent-filter>,让系统识别你的自定义Scheme。示例配置如下:

<activity
    android:name=".YourMainActivity"
    android:exported="true"> <!-- Android 12+ 必须设置 exported="true" 才能接收外部Intent -->
    <!-- 其他原有配置 -->
    
    <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="sosapp"
            android:host="sosapp"
            android:pathPattern="/hashcode/.*" /> <!-- 匹配/hashcode/开头的所有路径 -->
    </intent-filter>
</activity>

注意:

  • Android 12及以上版本,接收外部Intent的Activity必须设置android:exported="true",否则系统会拦截。
  • pathPattern 可以根据你的需求调整,比如如果你需要精确匹配某个哈希值,也可以写成android:path="/hashcode/ghfhgfhgf",但用.*更灵活。

2. 配置包可见性(Android 11+)

从Android 11开始,系统限制了应用对其他应用的可见性,你需要在AndroidManifest.xml中添加<queries>标签,声明你的应用需要响应自定义Scheme的Intent:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="sosapp" />
    </intent>
</queries>

3. 测试配置是否生效

用adb命令直接测试,看能否唤起应用:

adb shell am start -W -a android.intent.action.VIEW -d "sosapp://sosapp/hashcode/ghfhgfhgf" com.your.app.package

替换com.your.app.package为你的应用包名。如果命令能成功唤起应用,说明配置没问题,可能是浏览器的默认行为问题;如果失败,检查上述配置是否有误。


iOS 端配置排查

1. 配置 Info.plist 的 URL Types

在Xcode中,进入你的Target -> Info -> URL Types,点击+号添加一个新的URL类型:

  • Identifier:可以填任意唯一值,比如com.your.app.soslink
  • URL Schemes:填写sosapp(和你的DeepLink Scheme一致)

或者直接编辑Info.plist文件,添加以下内容:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.your.app.soslink</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>sosapp</string>
        </array>
    </dict>
</array>

2. 测试配置是否生效

在iOS模拟器或真机上,打开Notes应用,输入你的DeepLink sosapp://sosapp/hashcode/ghfhgfhgf 并点击,看是否弹出“打开在[你的应用名]”的选项。也可以用Xcode命令行测试:

xcrun simctl openurl booted "sosapp://sosapp/hashcode/ghfhgfhgf"

3. 处理Safari的特殊行为

Safari对自定义Scheme的处理比较严格,第一次点击可能不会直接唤起应用,需要用户长按链接选择“打开在[你的应用]”。如果想要更流畅的体验,可以考虑配置Universal Links(iOS 9+支持),通过域名验证的方式让Safari直接唤起应用,但这需要你拥有一个域名并配置关联文件。


通用注意事项

  • 链接格式规范:你的链接末尾有个多余的?,虽然不影响识别,但建议调整为标准格式,比如sosapp://sosapp/hashcode/ghfhgfhgf(无参数时)或sosapp://sosapp/hashcode/ghfhgfhgf?param=value(带参数时)。
  • 确保应用已正确安装:测试设备上必须安装包含上述配置的最新版本应用,旧版本可能没有添加DeepLink配置。
  • 浏览器兼容性:部分第三方浏览器可能会拦截自定义Scheme,建议多测试几个主流浏览器(比如Chrome、Firefox、系统自带浏览器)。

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

火山引擎 最新活动