我的App.js
中有以下代码:
import React, { useState, useRef, useEffect } from 'react';
import { SafeAreaView, Text } from 'react-native';
import { NavigationContainer, useLinking } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
const Stack = createStackNavigator();
const Screen1 = () => <SafeAreaView><Text>Screen1</Text></SafeAreaView>;
const Screen2 = () => <SafeAreaView><Text>Screen2</Text></SafeAreaView>;
export default function App() {
const ref = useRef();
const [isReady, setIsReady] = useState(false);
const [initialState, setInitialState] = useState();
const { getInitialState } = useLinking(ref, {
prefixes: ['http://example.com', 'mychat://'],
config: {
screens: {
Screen2: 'screen-2',
},
},
});
useEffect(() => {
getInitialState().then((state) => {
if (state !== undefined) setInitialState(state);
setIsReady(true);
});
}, [getInitialState]);
if (!isReady) return null;
return (
<NavigationContainer ref={ref} initialState={initialState}>
<Stack.Navigator>
<Stack.Screen name='Screen1' component={Screen1} />
<Stack.Screen name='Screen2' component={Screen2} />
</Stack.Navigator>
</NavigationContainer>
);
}
大部分是从https://reactnavigation.org/docs/deep-linking/和https://reactnavigation.org/docs/use-linking/复制的
文档中有prefixes: ['https://mychat.com', 'mychat://']
,我只是把https://mychat.com
改成了http://example.com
。但它似乎不起作用。
当我在Safari中打开以下链接时:
mychat://
(工作,被重定向到appScreen1)mychat://screen-2
(工作,被重定向到应用程序Screen2)http://example.com
(只是在浏览器中打开链接,没有弹出重定向到应用程序)
我需要做什么改变才能将域名重定向到移动应用程序?我错过什么了吗?
您需要使用与服务器一起访问的域。
您的服务器需要托管几个文件,通常在.well-known
目录中:
apple-app-site-association
(注意.json
不需要)assetlinks.json
你还需要在iOS应用程序中启用一些权限,这可能对Android也是如此。在iOS上,这将启用关联域权限以及webcredentials:yourdomain.com
条目。该文档非常好,可以帮助您了解实现通用链接需要做什么
- https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html
- https://developer.android.com/training/app-links/verify-site-associations
例子:
- iOS - https://stackoverflow.com/.well-known/apple-app-site-association
- Android - https://stackoverflow.com/.well-known/assetlinks.json