Expo WebBrowser
一个提供访问系统网页浏览器并支持处理重定向的库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-web-browser 提供对系统 Web 浏览器的访问,并支持处理重定向。在 Android 上,它使用 ChromeCustomTabs;在 iOS 上,它根据你调用的方法使用 SFSafariViewController 或 ASWebAuthenticationSession。从 iOS 11 起,SFSafariViewController 不再与 Safari 共享 cookie,因此如果你使用 WebBrowser 进行身份验证,应当使用 WebBrowser.openAuthSessionAsync;如果你只是想打开一个网页(例如应用的隐私政策),则使用 WebBrowser.openBrowserAsync。
安装
- npx expo install expo-web-browserIf you are installing this in an existing React Native app, make sure to install expo in your project.
在 app config 中配置
如果你在项目中使用 config plugin(Continuous Native Generation (CNG)),你可以使用 expo-web-browser 内置的 config plugin 进行配置。该插件允许你配置一些无法在运行时设置的属性,这些属性需要构建新的应用二进制文件后才会生效。如果你的应用没有使用 CNG,那么你需要手动配置该库。
用法
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="打开 WebBrowser" 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', }, });
处理来自 WebBrowser 的深度链接
如果你的项目使用 Expo Router,深度链接会自动处理。
如果你使用 WebBrowser 窗口进行身份验证,或在其他需要通过深度链接将信息传回应用的场景中,请在打开浏览器之前通过 Linking.addEventListener 添加一个处理器。当监听器触发时,你应该调用 dismissBrowser。在处理深度链接时,它不会自动关闭。除此之外,WebBrowser 的重定向与其他深度链接的工作方式相同。有关更多信息,请阅读 Linking。
API
import * as WebBrowser from 'expo-web-browser';
Methods
| Parameter | Type | Description |
|---|---|---|
| browserPackage(optional) | string | Package of browser to be cooled. If not set, preferred browser will be used. |
This methods removes all bindings to services created by warmUpAsync
or mayInitWithUrlAsync. You should call
this method once you don't need them to avoid potential memory leaks. However, those binding
would be cleared once your application is destroyed, which might be sufficient in most cases.
Promise<ServiceActionResult>The promise which fulfils with WebBrowserCoolDownResult when cooling is performed, or
an empty object when there was no connection to be dismissed.
Dismisses the current authentication session. On web, it will close the popup window associated with auth process.
voidThe void on the successful attempt or throws an error if dismiss functionality is not available.
Returns a list of applications package names supporting Custom Tabs, Custom Tabs
service, user chosen and preferred one. This may not be fully reliable, since it uses
PackageManager.getResolvingActivities under the hood. (For example, some browsers might not be
present in browserPackages list once another browser is set to default.)
Promise<WebBrowserCustomTabsResults>The promise which fulfils with WebBrowserCustomTabsResults object.
| Parameter | Type |
|---|---|
| options(optional) | WebBrowserCompleteAuthSessionOptions |
Possibly completes an authentication session on web in a window popup. The method should be invoked on the page that the window redirects to.
Returns an object with message about why the redirect failed or succeeded:
If type is set to failed, the reason depends on the message:
Not supported on this platform: If the platform doesn't support this method (Android, iOS).Cannot use expo-web-browser in a non-browser environment: If the code was executed in an SSR or node environment.No auth session is currently in progress: (the cached state wasn't found in local storage). This can happen if the window redirects to an origin (website) that is different to the initial website origin. If this happens in development, it may be because the auth started on localhost and finished on your computer port (Ex:128.0.0.*). This is controlled by theredirectUrlandreturnUrl.Current URL "<URL>" and original redirect URL "<URL>" do not match: This can occur when the redirect URL doesn't match what was initial defined as thereturnUrl. You can skip this test in development by passing{ skipRedirectCheck: true }to the function.
If type is set to success, the parent window will attempt to close the child window immediately.
If the error ERR_WEB_BROWSER_REDIRECT was thrown, it may mean that the parent window was
reloaded before the auth was completed. In this case you'll need to close the child window manually.
| Parameter | Type | Description |
|---|---|---|
| url | string | The url of page that is likely to be loaded first when opening browser. |
| browserPackage(optional) | string | Package of browser to be informed. If not set, preferred browser will be used. |
This method initiates (if needed) CustomTabsSession
and calls its mayLaunchUrl method for browser specified by the package.
Promise<ServiceActionResult>A promise which fulfils with WebBrowserMayInitWithUrlResult object.
| Parameter | Type | Description |
|---|---|---|
| url | string | The url to open in the web browser. This should be a login page. |
| redirectUrl(optional) | string | null | Optional - The url to deep link back into your app.
On web, this defaults to the output of |
| options(optional) | AuthSessionOpenOptions | Optional - An object extending the Default: {} |
On Android:
This will be done using a "custom Chrome tabs" browser, AppState, and Linking APIs.
On iOS:
Opens the url with Safari in a modal using ASWebAuthenticationSession. The user will be asked
whether to allow the app to authenticate using the given url.
To handle redirection back to the mobile application, the redirect URI set in the authentication server
has to use the protocol provided as the scheme in app.json expo.scheme.
For example, demo:// not https:// protocol.
Using Linking.addEventListener is not needed and can have side effects.
On web:
This API can only be used in a secure environment (localhost/https). to test this. Otherwise, an error with code
ERR_WEB_BROWSER_CRYPTOwill be thrown. This will use the browser'swindow.open()API.
- Desktop: This will create a new web popup window in the browser that can be closed later using
WebBrowser.maybeCompleteAuthSession(). - Mobile: This will open a new tab in the browser which can be closed using
WebBrowser.maybeCompleteAuthSession().
How this works on web:
- A crypto state will be created for verifying the redirect.
- This means you need to run with
npx expo start --https
- This means you need to run with
- The state will be added to the window's
localstorage. This ensures that auth cannot complete unless it's done from a page running with the same origin as it was started. Ex: ifopenAuthSessionAsyncis invoked onhttps://localhost:19006, thenmaybeCompleteAuthSessionmust be invoked on a page hosted from the originhttps://localhost:19006. Using a different website, or even a different host likehttps://128.0.0.*:19006for example will not work. - A timer will be started to check for every 1000 milliseconds (1 second) to detect if the window
has been closed by the user. If this happens then a promise will resolve with
{ type: 'dismiss' }.
On mobile web, Chrome and Safari will block any call to
window.open()which takes too long to fire after a user interaction. This method must be invoked immediately after a user interaction. If the event is blocked, an error with codeERR_WEB_BROWSER_BLOCKEDwill be thrown.
Promise<WebBrowserAuthSessionResult>- If the user does not permit the application to authenticate with the given url, the Promise fulfills with
{ type: 'cancel' }object. - If the user closed the web browser, the Promise fulfills with
{ type: 'cancel' }object. - If the browser is closed using
dismissBrowser, the Promise fulfills with{ type: 'dismiss' }object.
| Parameter | Type | Description |
|---|---|---|
| url | string | The url to open in the web browser. |
| browserParams(optional) | WebBrowserOpenOptions | A dictionary of key-value pairs. Default: {} |
Opens the url with Safari in a modal on iOS using SFSafariViewController,
and Chrome in a new custom tab
on Android. On iOS, the modal Safari will not share cookies with the system Safari. If you need
this, use openAuthSessionAsync.
Promise<WebBrowserResult>The promise behaves differently based on the platform.
On Android promise resolves with { type: 'opened' } if we were able to open browser.
On iOS:
- If the user closed the web browser, the Promise resolves with
{ type: 'cancel' }. - If the browser is closed using
dismissBrowser, the Promise resolves with{ type: 'dismiss' }.
| Parameter | Type | Description |
|---|---|---|
| browserPackage(optional) | string | Package of browser to be warmed up. If not set, preferred browser will be warmed. |
This method calls the warmUp method on CustomTabsClient
for specified package.
Promise<ServiceActionResult>A promise which fulfils with WebBrowserWarmUpResult object.
Types
If there is no native AuthSession implementation available (which is the case on Android) the params inherited from
WebBrowserOpenOptions will be used in the browser polyfill. Otherwise, the browser parameters will be ignored.
Type: WebBrowserOpenOptions extended by:
| Property | Type | Description |
|---|---|---|
| preferEphemeralSession(optional) | boolean | Only for: iOS Determines whether the session should ask the browser for a private authentication session.
Set this to Default: false |
| preferUniversalLinks(optional) | boolean | Only for: iOS Whether to use HTTPS universal link callbacks instead of custom URL scheme callbacks.
Set this to When Default: false |
Literal Type: union
Acceptable values are: WebBrowserRedirectResult | WebBrowserResult
| Property | Type | Description |
|---|---|---|
| skipRedirectCheck(optional) | boolean | Attempt to close the window without checking to see if the auth redirect matches the cached redirect URL. |
| Property | Type | Description |
|---|---|---|
| message | string | Additional description or reasoning of the result. |
| type | 'success' | 'failed' | Type of the result. |
| Property | Type | Description |
|---|---|---|
| browserPackages | string[] | All packages recognized by |
| defaultBrowserPackage(optional) | string | Default package chosen by user, |
| preferredBrowserPackage(optional) | string | Package preferred by |
| servicePackages | string[] | All packages recognized by |
| Property | Type | Description |
|---|---|---|
| browserPackage(optional) | string | Only for: Android Package name of a browser to be used to handle Custom Tabs. List of
available packages is to be queried by |
| controlsColor(optional) | string | Only for: iOS Tint color for controls in SKSafariViewController. Supports React Native color formats. |
| createTask(optional) | boolean | Only for: Android A boolean determining whether the browser should open in a new task or in the same task as your app. Default: true |
| dismissButtonStyle(optional) | 'done' | 'close' | 'cancel' | Only for: iOS The style of the dismiss button. Should be one of: |
| enableBarCollapsing(optional) | boolean | A boolean determining whether the toolbar should be hiding when a user scrolls the website. |
| enableDefaultShareMenuItem(optional) | boolean | Only for: Android A boolean determining whether a default share item should be added to the menu. |
| presentationStyle(optional) | WebBrowserPresentationStyle | Only for: iOS The presentation style of the browser window. Default: WebBrowser.WebBrowserPresentationStyle.OverFullScreen |
| readerMode(optional) | boolean | Only for: iOS A boolean determining whether Safari should enter Reader mode, if it is available. |
| secondaryToolbarColor(optional) | string | Only for: Android Color of the secondary toolbar. Supports React Native color formats. |
| showInRecents(optional) | boolean | Only for: Android A boolean determining whether browsed website should be shown as separate
entry in Android recents/multitasking view. Requires Default: false |
| showTitle(optional) | boolean | Only for: Android A boolean determining whether the browser should show the title of website on the toolbar. |
| toolbarColor(optional) | string | Color of the toolbar. Supports React Native color formats. |
| useProxyActivity(optional) | boolean | Only for: Android A boolean determining whether to use a proxy activity to launch the browser.
It's available only when Set to Default: true |
| windowFeatures(optional) | string | WebBrowserWindowFeatures | Only for: Web Features to use with |
| windowName(optional) | string | Only for: Web Name to assign to the popup window. |
| Property | Type | Description |
|---|---|---|
| type | 'success' | Type of the result. |
| url | string | - |
| Property | Type | Description |
|---|---|---|
| type | WebBrowserResultType | Type of the result. |
Enums
A browser presentation style. Its values are directly mapped to the UIModalPresentationStyle.
WebBrowserPresentationStyle.AUTOMATIC = "automatic"The default presentation style chosen by the system.
On older iOS versions, falls back to WebBrowserPresentationStyle.FullScreen.
WebBrowserPresentationStyle.CURRENT_CONTEXT = "currentContext"A presentation style where the browser is displayed over the app's content.
WebBrowserPresentationStyle.FORM_SHEET = "formSheet"A presentation style that displays the browser centered in the screen.
WebBrowserPresentationStyle.FULL_SCREEN = "fullScreen"A presentation style in which the presented browser covers the screen.
WebBrowserPresentationStyle.OVER_CURRENT_CONTEXT = "overCurrentContext"A presentation style where the browser is displayed over the app's content.
WebBrowserPresentationStyle.OVER_FULL_SCREEN = "overFullScreen"A presentation style in which the browser view covers the screen.
WebBrowserPresentationStyle.PAGE_SHEET = "pageSheet"A presentation style that partially covers the underlying content.
错误代码
ERR_WEB_BROWSER_REDIRECT
仅限 Web: 该窗口无法完成重定向请求,因为调用它的窗口没有指向其父窗口的引用。如果父窗口已重新加载,就可能发生这种情况。
ERR_WEB_BROWSER_BLOCKED
仅限 Web: 弹出窗口被浏览器阻止或未能打开。如果在用户输入触发后过了太久才调用 window.open() 方法,这种情况可能会发生在移动浏览器中。
移动浏览器这样做是为了防止恶意网站在移动设备上打开大量不需要的弹窗。
你的方法仍然可以在异步函数中运行,但在此之前不能有任何耗时较长的任务。你可以使用 hooks 在其他进程完成加载之前禁用用户输入。
ERR_WEB_BROWSER_CRYPTO
仅限 Web: 当前环境不支持 crypto。请确保你是在安全源(localhost/https)下运行。