This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo WebBrowser iconExpo WebBrowser

提供对系统 Web 浏览器的访问并支持处理重定向的库。

Android
iOS
Web
Included in Expo Go
Recommended version:
~58.0.6

expo-web-browser 提供对系统网页浏览器的访问,并支持处理重定向。在 Android 上,它使用 ChromeCustomTabs,在 iOS 上则根据你调用的方法使用 SFSafariViewController 或 ASWebAuthenticationSession。自 iOS 11 起,SFSafariViewController 不再与 Safari 共享 Cookies,因此如果你使用 WebBrowser 进行身份验证,你将需要使用 WebBrowser.openAuthSessionAsync;如果你只是想打开网页(例如你的应用隐私政策),则使用 WebBrowser.openBrowserAsync。

安装

🌐 Installation

Terminal
- npx expo install expo-web-browser
- yarn expo install expo-web-browser
- pnpm expo install expo-web-browser
- bun expo install expo-web-browser

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

🌐 Configuration in app config

如果你在项目中使用配置插件(连续原生生成 (CNG)),你可以使用其内置的 配置插件 来配置 expo-web-browser。该插件允许你配置一些无法在运行时设置的属性,这些属性需要构建新的应用二进制文件才能生效。如果你的应用不使用 CNG,那么你需要手动配置该库。

🌐 You can configure expo-web-browser using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.

用法

🌐 Usage

Basic WebBrowser usage
import { useState } from 'react'; import { Button, Text, View, StyleSheet } from 'react-native'; import * as WebBrowser from 'expo-web-browser'; %%placeholder-start%%%%placeholder-end%%import Constants from 'expo-constants'; export default function App() { const [result, setResult] = useState(null); const _handlePressButtonAsync = async () => { let result = await WebBrowser.openBrowserAsync('https://expo.dev'); setResult(result); }; return ( <View style={styles.container}> <Button title="打开网页浏览器" onPress={_handlePressButtonAsync} /> <Text>{result && JSON.stringify(result)}</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', paddingTop: Constants.statusBarHeight, backgroundColor: '#ecf0f1', }, });

处理来自 Web 浏览器的深层链接

🌐 Handling deep links from the WebBrowser

如果你的项目使用 Expo Router,深层链接将自动处理。

🌐 If your project uses Expo Router, deep links are handled automatically.

应用接口

🌐 API

import * as WebBrowser from 'expo-web-browser';

No API data file found, sorry!

错误代码

🌐 Error codes

ERR_WEB_BROWSER_REDIRECT

仅限网页: 窗口无法完成重定向请求,因为调用窗口没有其父窗口的引用。如果父窗口已重新加载,就可能发生这种情况。

ERR_WEB_BROWSER_BLOCKED

仅限网页: 弹出窗口被浏览器阻止或未能打开。当在移动浏览器中调用 window.open() 方法距离用户操作触发时间过长时,可能会出现这种情况。

移动浏览器这样做是为了防止恶意网站在移动设备上打开许多不需要的弹出窗口。

🌐 Mobile browsers do this to prevent malicious websites from opening many unwanted popups on mobile.

你的方法仍然可以在异步函数中运行,但在它之前不能有任何长时间运行的任务。你可以使用钩子在其他进程加载完成之前禁用用户输入。

🌐 You're method can still run in an async function but there cannot be any long running tasks before it. You can use hooks to disable user-inputs until any other processes have finished loading.

ERR_WEB_BROWSER_CRYPTO

仅限网页: 当前环境不支持加密。请确保你正在从安全来源运行(本地主机/https)。