我已经将我的 React Native 应用程序设置为将深度链接与 expo-linking 结合使用,但由于某种原因,它无法在 Android 上运行(尚未在 iOS 上实现)。打开链接只会在网络浏览器中打开它,而不会按应有的方式打开应用程序。知道为什么吗?
应用程序.json
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#FFFFFF"
},
"package": "com.example.myapp",
"intentFilters": [
{
"action": "VIEW",
"data": [
{
"scheme": "https",
"host": "testlink.com",
}
],
"category": [
"BROWSABLE",
"DEFAULT"
]
}
]
},
Run Code Online (Sandbox Code Playgroud)
这并没有更新AndroidManifest,所以我手动编辑了它:
AndroidManifest.xml
<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="https" android:host="testlink.com"/>
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
应用程序.js
const linking = {
prefixes: ["https://testlink.com"],
};
useEffect(() => {
Linking.addEventListener("url", handleDeepLink);
return () => { …Run Code Online (Sandbox Code Playgroud) 我正在从 URI 下载文件,并尝试在另一个应用程序中打开它。该文件是一个简单的 .xlsx 电子表格。我正在使用 Android 设备进行测试。下面的代码是一个可重现的示例:
import React, { useCallback } from 'react'
import { Button, View } from 'react-native'
import * as Linking from 'expo-linking'
import * as FileSystem from 'expo-file-system'
const App = () => {
const download = useCallback(async () => {
const downloadUri = 'http://url.to/my/spreadsheet.xlsx')
const localPath = `${FileSystem.cacheDirectory}spreadsheet.xlsx`
try {
await FileSystem.downladAsync(downloadUri, localPath)
.then(async ({ uri }) => {
const contentURL = await FileSystem.getContentUriAsync(uri)
await Linking.openURL(contentURL)
})
.catch((err) => console.log(err))
} catch (err) { …Run Code Online (Sandbox Code Playgroud) 我们将本机 Android 应用程序转换为 React 本机应用程序,并为新应用程序使用了 expo 管理的工作流程。对于深度链接,我们使用了博览会的链接文档,一切正常。但最近我们注意到,对于某些用户(特别是三星设备),Android 应用程序对所有 URL(对于配置的域)开放。
意图过滤器设置:
{
"expo": {
"android": {
"intentFilters": [
{
"action": "VIEW",
"data": [
{
"scheme": "https",
"host": "*.domain.com",
"pathPrefix": "/jobs"
},
{
"scheme": "https",
"host": "*.domain.com",
"pathPrefix": "/can/recommended-jobs"
},
{
"scheme": "https",
"host": "*.domain.com",
"pathPrefix": "/can/cv-views"
},
{
"scheme": "https",
"host": "*.domain.com",
"pathPrefix": "/can/conversations"
},
{
"scheme": "https",
"host": "*.domain.com",
"pathPrefix": "/can/profile/basic-info"
},
{
"scheme": "https",
"host": "*.domain.com",
"pathPattern": "/.*/jobs/.*-.*"
},
{
"scheme": "https",
"host": "*.domain.com",
"pathPrefix": "/mobile/can/profile"
}, …Run Code Online (Sandbox Code Playgroud) android react-native expo react-native-deep-linking expo-linking
React Native包expo-linking有一个名为 的钩子,useURL当应用程序在后台运行时,该钩子对我不起作用。从文档来看,Returns the initial URL followed by any subsequent changes to the URL.我的托管博览会应用程序遇到的问题是,当应用程序已在后台打开时,挂钩不起作用。这是钩子:
export default function App() {
const isLoadingComplete = useCachedResources();
const url = Linking.useURL();
useEffect(() => {
Alert.alert(url ? url.substring(20) : 'null');
}, [url]);
if (!isLoadingComplete) {
return null;
} else {
return (
...
);
}
}
Run Code Online (Sandbox Code Playgroud)
exp://exp.host/@myprofile/myproject?a=b如果我在应用程序关闭时打开 URL ,我会按预期收到警报。如果应用程序在后台运行,则警报不会响起。我已经在 iOS 模拟器和物理 Android 上进行了测试。有什么解决办法吗?请注意,类似的问题也发生在Linking.addEventListener().