跳到主要内容
版本:v2

浏览器

Browser API 使得打开应用内浏览器会话以显示外部 Web 内容、处理认证流程等变得简单。

在 iOS 上,它使用 SFSafariViewController,并符合主流 oAuth 服务的应用内浏览器要求。

import { Plugins } from '@capacitor/core';

const { Browser } = Plugins;

await Browser.open({ url: 'http://capacitorjs.com/' });

API

open(...)

open(options: BrowserOpenOptions) => Promise<void>

使用给定的 URL 打开页面

参数类型
options
BrowserOpenOptions

prefetch(...)

prefetch(options: BrowserPrefetchOptions) => Promise<void>

提示浏览器将要访问给定的 URL,以改善初始加载时间。

仅在 Android 上有效,在 iOS 上是空操作

参数类型
options
BrowserPrefetchOptions

close()

close() => Promise<void>

关闭打开的浏览器。仅在 iOS 和 Web 环境中有效,否则是空操作


addListener(...)

addListener(eventName: 'browserFinished', listenerFunc: (info: any) => void) => PluginListenerHandle
参数类型
eventName"browserFinished"
listenerFunc(info: any) => void

返回:

PluginListenerHandle


addListener(...)

addListener(eventName: 'browserPageLoaded', listenerFunc: (info: any) => void) => PluginListenerHandle
参数类型
eventName"browserPageLoaded"
listenerFunc(info: any) => void

返回:

PluginListenerHandle


removeAllListeners()

removeAllListeners() => void

移除该插件的所有原生监听器


接口

BrowserOpenOptions

属性类型描述
urlstring浏览器要打开的 URL
windowNamestring仅 Web:浏览器打开的可选目标。遵循 window.open 的 target 属性。默认为 _blank
toolbarColorstring设置工具栏颜色的十六进制颜色值。
presentationStyle"fullscreen" | "popover"仅 iOS:浏览器的呈现样式。默认为 fullscreen。

BrowserPrefetchOptions

属性类型
urlsstring[]

PluginListenerHandle

属性类型
remove() => void