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
提供对系统 Web 浏览器的访问并支持处理重定向的库。
expo-web-browser 提供对系统网页浏览器的访问,并支持处理重定向。在 Android 上,它使用 ChromeCustomTabs,在 iOS 上则根据你调用的方法使用 SFSafariViewController 或 ASWebAuthenticationSession。自 iOS 11 起,SFSafariViewController 不再与 Safari 共享 Cookies,因此如果你使用 WebBrowser 进行身份验证,你将需要使用 WebBrowser.openAuthSessionAsync;如果你只是想打开网页(例如你的应用隐私政策),则使用 WebBrowser.openBrowserAsync。
安装
🌐 Installation
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
处理来自 Web 浏览器的深层链接
🌐 Handling deep links from the WebBrowser
如果你的项目使用 Expo Router,深层链接将自动处理。
🌐 If your project uses Expo Router, deep links are handled automatically.
如果你使用 WebBrowser 窗口进行认证或其他需要通过深度链接将信息传回应用的场景,请在打开浏览器之前使用 Linking.addEventListener 添加处理程序。当监听器触发时,你应当调用 dismissBrowser。在处理深度链接时,它不会自动关闭。除此之外,来自 WebBrowser 的重定向与其他深度链接的处理方式相同。详情请参阅 Linking。
🌐 If you use the WebBrowser window for authentication or another use case where you want to pass information back into your app through a deep link, add a handler with Linking.addEventListener before opening the browser. When the listener fires, you should call dismissBrowser. It will not automatically be dismissed when a deep link is handled. Aside from that, redirects from WebBrowser work the same as other deep links. Read more about it in Linking.
应用接口
🌐 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)。