メインコンテンツまでスキップ
バージョン: v5

Capacitor iOS API

Capacitor iOS is the native runtime that powers Capacitor apps on iOS.

Bridge

The iOS bridge is the heart of the Capacitor iOS library. There are several properties and methods available on the bridge which provide information or change behavior.

When registered with Capacitor, plugins have a weak reference to the bridge:

self.bridge?

If your method requires the bridge, you can use a guard to unwrap it and perform an early exit:

guard let bridge = self.bridge else { return }

viewController

var viewController: UIViewController? { get }

This property contains the main view controller for Capacitor, which can be used to present native views over the app.

Examples:

DispatchQueue.main.async {
self.bridge?.viewController.present(ourCustomViewController, animated: true, completion: nil)
}

On iPad devices it is possible to present popovers:

self.setCenteredPopover(ourCustomViewController)
self.bridge.viewController.present(ourCustomViewController, animated: true, completion: nil)

config

var config: InstanceConfiguration { get }

This property contains the configuration object known to the Capacitor runtime.


triggerJSEvent(...)

func triggerJSEvent(eventName: String, target: String)
func triggerJSEvent(eventName: String, target: String, data: String)

Fire an event on a JavaScript EventTarget such as window or document. If possible, it is preferred to use Plugin Events instead.

Examples:

bridge.triggerJSEvent(eventName: "myCustomEvent", target: "window")
bridge.triggerJSEvent(eventName: "myCustomEvent", target: "document", data: "{ 'dataKey': 'dataValue' }")

Note: data must be a serialized JSON string value.


localURL(...)

func localURL(fromWebURL webURL: URL?) -> URL?

Translate a URL from the web view into a file URL for native iOS.

The web view may be handling several different types of URLs:

  • res:// (shortcut scheme to web assets)
  • file:// (fully qualified URL to file on the local device)

portablePath(...)

func portablePath(fromLocalURL localURL: URL?) -> URL?

Translate a file URL for native iOS into a URL to load in the web view.


Passing data

Notes on how to work with data that is passed between environments can be found here.


Saving CAPPluginCall

Notes on persisting plugin calls for asynchronous or repeated operations can be found here.