我想用 React Native 打开一个 url。但我不想进入浏览器页面。当我单击按钮时,它应该在后台打开一个网址。但我想留在我的应用程序中。有没有办法通过博览会做到这一点?
我寻找了很长时间的结果,但没有找到任何解决方案。 我尝试过background-feth、react native-linking、window.open等等。
由于安全性和限制,在 React Native (Expo) 中,在后台打开 URL 而不切换到浏览器页面有点棘手。 Expo 没有提供实现此行为的直接方法,因为这违反了他们的安全原则。
但是,您可以使用
InAppBrowser
模块在应用程序中打开 URL
。该模块允许您在 React Native 应用程序中显示 Web 内容。
可以参考[这个][1]
您可以使用以下方式安装它:
expo install expo-in-app-browser
例如:-
import React from 'react';
import { View, Button } from 'react-native';
import * as WebBrowser from 'expo-in-app-browser';
const App = () => {
const openURL = async () => {
try {
await WebBrowser.openBrowserAsync('https://www.google.com', {
});
} catch (error) {
console.error('Error opening URL', error);
}
};
return (
<View>
<Button title="Open URL" onPress={openURL} />
</View>
);
};
export default App;