接入 Expo 移动壳深链入口

新增移动壳 deep link 到同源 H5 路径的解析与运行时监听

配置移动壳真实品牌图标、iOS associated domain 和 Android app link

补充移动壳配置守门、单测和宿主壳文档记忆
This commit is contained in:
2026-06-17 22:04:18 +08:00
parent 4acc81747a
commit 02a475d652
9 changed files with 252 additions and 15 deletions

View File

@@ -8,28 +8,58 @@ import {
handleMobileHostBridgeMessage,
MOBILE_HOST_CAPABILITIES,
} from './src/mobileHostBridge';
import { buildMobileShellUrlFromDeepLink } from './src/mobileShellDeepLink';
import { shouldOpenInMobileShellWebView } from './src/mobileShellNavigation';
import { buildMobileShellUrl } from './src/mobileShellUrl';
const defaultWebUrl = 'http://127.0.0.1:3000/';
const hostVersion = '0.1.0';
export default function App() {
const webViewRef = useRef<WebView>(null);
const [canGoBack, setCanGoBack] = useState(false);
const webUrl = useMemo(
() =>
buildMobileShellUrl(
process.env.EXPO_PUBLIC_GENARRATIVE_WEB_URL || defaultWebUrl,
{
platform: Platform.OS === 'ios' ? 'ios' : 'android',
hostVersion: '0.1.0',
capabilities: MOBILE_HOST_CAPABILITIES,
},
),
const baseWebUrl = process.env.EXPO_PUBLIC_GENARRATIVE_WEB_URL || defaultWebUrl;
const mobileShellUrlOptions = useMemo(
() => ({
platform: Platform.OS === 'ios' ? 'ios' as const : 'android' as const,
hostVersion,
capabilities: MOBILE_HOST_CAPABILITIES,
}),
[],
);
const [webUrl, setWebUrl] = useState(() =>
buildMobileShellUrl(baseWebUrl, mobileShellUrlOptions),
);
const allowedWebOrigin = useMemo(() => new URL(webUrl).origin, [webUrl]);
useEffect(() => {
let disposed = false;
const openDeepLink = (url: string | null | undefined) => {
const nextUrl = buildMobileShellUrlFromDeepLink(
url,
baseWebUrl,
mobileShellUrlOptions,
);
setWebUrl(nextUrl);
};
void Linking.getInitialURL().then((url) => {
if (!disposed) {
openDeepLink(url);
}
});
const subscription = Linking.addEventListener('url', (event) => {
openDeepLink(event.url);
});
return () => {
disposed = true;
subscription.remove();
};
}, [baseWebUrl, mobileShellUrlOptions]);
useEffect(() => {
const subscription = BackHandler.addEventListener(
'hardwareBackPress',