react-native 设置url scheme (react-navigation)

通过react-navigation设置打开APP的scheme
react-navigation官网(不知道什么时候更新了,我还在用1.5.11)[https://reactnavigation.org/docs/en/deep-linking.html]

一、ios

1. 设置path

(通过如path:"Complaint",则启动的时候是:你的scheme://Complaint)
配置path(具体语法可以参考官网):

const RouteConfigs = {
Complaint: {
        screen: Complaint,//投诉
        path: 'Complaint',
    }
}

2. 设置URI Prefix

在react-navigation 入口处添加下面内容:

import {Platform} from "react-native";

const prefix = Platform.OS == 'android' ? 'mychat://mychat/' : 'mychat://';
//...others code 
// 我的是在根目录下的app.js,<App/> 是我的入口
 <App uriPrefix={prefix} />

3. 在xcode中打开AppDelegate.m文件,如下图:

AppDelegate.m
#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
// 在这里添加这句
#import <React/RCTLinkingManager.h>
@implementation AppDelegate
//省略其他的代码

// 在@end前台添加上下面这段
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [RCTLinkingManager application:application openURL:url
                      sourceApplication:sourceApplication annotation:annotation];
}
@end
按照图片上的修改

4. 直接在模拟器上测试

  1. react-native run-ios;
  2. xcrun simctl openurl booted zhlx://Complaint

输入内容回车即可:


image.png

二、Android

1. 对于Android的话,和ios有点不太一样,就是需要设置host,通过在APP入口文件添加的

const prefix = Platform.OS == 'android' ? 'mychat://mychat/' : 'mychat://';

有童鞋就已经猜出来了。

2. android/app/src/main/AndroidManifest.xml中添加intent-filter;

个人不懂Android(web前端),所以在使用的时候遇到了个坑,如下所示,我将官网让放的intent-filter放到了一个intent-filter里面(示例1),结果运行打包什么的都没问题,但是打包成apk安装到手机后才发现APP的icon没了,桌面找不到,只能在设置里面看到,改为(示例2)后才正确使用。

示例1:

 <intent-filter>
                <!--这里是原来就有的-->
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                <!--这里是react-navigation 让添加的-->
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="mychat" android:host="mychat"/>
            </intent-filter>

示例2:

 <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="mychat" android:host="mychat"/>
                <!-- 这里结束 -->
            </intent-filter>

官网截图:


官网截图

3.运行程序测试

  • 运行 react-native run-android
  • adb shell am start -W -a android.intent.action.VIEW -d "mychat://mychat/chat/Eric" com.simpleapp

mychat://mychat/chat/Eric 就是在浏览器或者其他外部打开并跳转到APP的链接;

大家可以对比参考ios和Android的scheme:

end !

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 持续更新中...... 一套企业级的 UI 设计语言和 React 实现。 https://mobile.ant....
    日不落000阅读 11,166评论 0 35
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,091评论 3 119
  • react-native.jpeg 本文是基于最新的react-navigation^2.9.1来书写的。 如果遇...
    挂着铃铛的兔阅读 221,572评论 131 299
  • 一直以来,喜欢吃肉,水晶包,五花肉,红烧肉,五花腊肉…… 以上的肉,最好是肥瘦相间的最好吃了。 每每与朋友谈论起红...
    华华雯雯阅读 2,607评论 0 3
  • 春天里,宅在家里好久的我,终于可以走出去,和朋友们一起去武汉周边爬山了。沿途的车上,装满了孩子们快乐的欢笑声,还有...
    职场妖猫姐阅读 3,940评论 2 6

友情链接更多精彩内容