Laptop mit Handy App und Code
Jonathan, Kiya | 11.08.2023

Creating a Custom Capacitor Plugin with Native Swift Code for iOS

Mobile > Creating a Custom Capacitor Plugin with Native Swift Code for iOS

A custom plugin requires two swift files:

  1. ARPlugin.swift

1import Capacitor
2
3@objc(ARPlugin)
4public class ARPlugin: CAPPlugin {
5    @objc func open(_ call: CAPPluginCall) {
6        DispatchQueue.main.async {
7            let arViewController: ARViewController = UIStoryboard(name: "AugmentedReality", bundle: nil).instantiateInitialViewController()!
8            arViewController.eventListeners = self.eventListeners!
9            self.bridge?.viewController?.present(arViewController, animated: true, completion: nil)
10        }
11    }
12}

This file is where the methods that will later be executable from the TypeScript code are written.

2. ARPlugin.m

1#import <Capacitor/Capacitor.h>
2
3CAP_PLUGIN(ARPlugin, "ARPlugin", CAP_PLUGIN_METHOD(open, CAPPluginReturnNone);)

These methods are then registered with Capacitor.

In the Web App, the plugin is then used as follows:

1import { registerPlugin } from '@capacitor/core';
2
3const ARPlugin = registerPlugin('ARPlugin')
4
5await ARPlugin.open()

Source: Capacitor Docs - Custom Native iOS Code

Content
  • What are the required Swift files?
  • How to write methods to be executed from TypeScript Code?
  • How to register the methods with Capacitor?
  • How to use the Plugin in a Web App?
Headshot of Jonathan Zbick
Jonathan (Dualer Student)

... ist dualer Student für IT- und Softwaresysteme am Standort Dortmund. Sein Schwerpunkt liegt in der Frontendentwicklung mit Angular, React und NodeJS. Stimmige Nutzungsabläufe und gute Usability si... mehr anzeigen

Kiya

... ist unsere engagierte und leidenschaftliche Künstliche Intelligenz und Expertin für Softwareentwicklung. Mit einem unermüdlichen Interesse für technologische Innovationen bringt sie Enthusiasmus u... mehr anzeigen

Standort Hannover

newcubator GmbH
Bödekerstraße 22
30161 Hannover

Standort Dortmund

newcubator GmbH
Westenhellweg 85-89
44137 Dortmund