推荐使用react-navigation导航器提供的Deep linking 功能来实现。
根据官方的例子来一步步实现:
假设我们要实现在浏览器上通过点击URI(mychat://chat/Eric
) ,跳转到一个名为“Eric”的用户的聊天页面。
1. 首先,我们定义导航器:
const SimpleApp = createStackNavigator({
Home: { screen: HomeScreen },
Chat: { screen: ChatScreen },
});
2. 要将“Eric”用户作为参数进行传递我们需要设置path:
const SimpleApp = createStackNavigator({
Home: { screen: HomeScreen },
Chat: {
screen: ChatScreen,
path: 'chat/:user',
},
});
3. 设置URI Prefix,配置导航容器获取URI 打开应用程序的路径前缀:
const SimpleApp = createStackNavigator({...});
// on Android, the URI prefix typically contains a host in addition to scheme
// on Android, note the required / (slash) at the end of the host property
const prefix = Platform.OS == 'android' ? 'mychat://mychat/' : 'mychat://';
const MainApp = () => <SimpleApp uriPrefix={prefix} />;
4. IOS 原生配置
在SimpleApp/ios/SimpleApp/AppDelegate.m
:
// Add the header at the top of the file:
#import <React/RCTLinkingManager.h>
// Add this above the `@end`:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [RCTLinkingManager application:application openURL:url
sourceApplication:sourceApplication annotation:annotation];
}
在Xcode中,打开项目SimpleApp/ios/SimpleApp.xcodeproj
。在侧栏中选择项目并导航到信息选项卡。向下滚动到“URL类型”并添加一个。在新的URL类型中,将标识符和url方案设置为所需的URL方案。
现在您可以在Xcode中按运行,或者在命令行上重新构建:
react-native run-ios
要在模拟器上测试URI,请运行以下命令:
xcrun simctl openurl booted mychat://chat/Eric
要在真实设备上测试URI,请打开Safari并键入mychat://chat/Eric
5.Android原生配置
在SimpleApp/android/app/src/main/AndroidManifest.xml
配置清单文件中,在MainActivity的activity中配置intent-fllter:
<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>
现在,重新安装应用程序:
react-native run-android
要在Android中测试意图处理,请运行以下命令:
adb shell am start -W -a android.intent.action.VIEW -d "mychat://mychat/chat/Eric" com.simpleapp
6. 可以使用新建一个html文件,在浏览器上测试唤醒跳转功能:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p><a href="mychat://mychat/chat/Eric">跳转安卓-APP进入Eric聊天页</a></p>
<p><a href="mychat://chat/Eric">跳转IOS-APP进入Eric聊天页</a></p>
</body>
</html>
7. 另外,可以在唤醒APP时通过RN API “Linking” 获取打开APP的链接信息,也可以自由设计跳转到指定的页面:
componentDidMount() {
Linking.getInitialURL().then((url) => {
if (url) {
console.log('Initial url is: ' + url);
}
}).catch(err => console.error('An error occurred', err));
}
let deepLinkURL = ‘mychat://mychat/指定页面’;
Linking.openURL(deepLinkURL).catch(err => console.error('An error occurred', err));
8. APP唤醒后获取URL,可以将URL携带的参数转为对象进行下一步的传值工作:
// 取URL地址参数转为对象
handleUrl(url) {
const urlObj = {};
const middleStr = url.split('?');
const paramPairStr = middleStr[1].split('&');
paramPairStr.forEach((element) => {
const singleParamStr = element.split('=');
urlObj[singleParamStr[0]] = singleParamStr[1];
});
return urlObj;
}
跳转指定页面成功,同时我们也可以拿到user的参数为”Eric“。赶紧试试吧!