在React Native的App中添加配置以支持能够从H5中唤醒(URL Scheme)

本文介绍了如何在Android原生和React Native应用中配置URL Scheme和Universal Link,以实现H5页面点击后跳转到APP内指定页面。在AndroidManifest.xml中添加相应的intent-filter,并在React Native的App.js中监听和处理唤醒URL,确保应用能正确响应H5的链接点击。
摘要由CSDN通过智能技术生成

相关博文:H5页面在浏览器中跳转APP的方式/URL Scheme/Universal Link

相关版本:

​ “react-native”: “0.61.5”

Android原生

AndroidManifest.xml中添加如下代码,如果已经存在了MainActivity就在里面添加。

<activity
          android:name="com.xxx.MainActivity"
          android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </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" />
        <data android:scheme="name" /> // 唤醒的路径,例如name://
    </intent-filter>
</activity>

React Native

  1. App.js配置未打开App时的第一次唤醒。

    Linking.getInitialURL()拿到唤醒的url,如果没有返回空。

    import { Linking } from "react-native";
    componentDidMount() {
    	Linking.getInitialURL()
                .then(url => {
                    if (url) {
                        this.handleDeepLinking({ url }); // 一些需要的操作
                    }
                })
    }
    
  2. 在需要对唤醒做出响应的页面进行监听,一般在App.js

    componentDidMount() {
        Linking.addEventListener('url', this.handleDeepLinking);
    }
    componentWillUnmount() {
        Linking.removeEventListener('url');
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值