Skip to content

Standalone Plugin Development

Create Plugin Project

  • In Xcode, choose New → Project
    Project option
  • Choose iOS Framework type, then Next
    Choose iOS Framework type
  • Enter plugin name — no Chinese. Example: hplugin. Other options as shown. Change Bundle Identifier as needed
    Enter name
  • Project structure:
    Project structure

Add Framework

  • Copy downloaded pluginhost.framework into the project directory
    pluginhost
  • Add pluginhost.framework reference in Xcode
    pluginhost
  • Click +, choose Add Files, then select pluginhost.framework
    pluginhost
pluginhost - Set pluginhost.framework to **Do Not Embed**
pluginhost

Create Plugin Class

  • Right-click the Xcode project, choose New File
    pluginhost
  • Choose Swift File
    pluginhost
  • Name it Plugin1 (or another name). Class content:
    pluginhost

Method Override Reference

// Comments based on demo code
import Foundation
import pluginhost
import JavaScriptCore
import UIKit
// Inheriting ECPlugin means the interface is ECPlugin, used as reflection base
// Inheriting JSExport means methods in this class can be called from JS
public class Plugin1 : NSObject, ECPlugin,JSExport{
// Corresponds to pluginLoader.callMethodAny
public func callMethodAny(_ methodName: String, _ data: Any) -> Any? {
print("callMethodAny --- ",data)
if(data is String){
print("any is string...")
}else if (data is UIImage){
print("any is ui image ...")
}
return ""
}
// Corresponds to pluginLoader.callMethodStr
public func callMethodStr(_ methodName: String, _ args: String) -> String {
return " callMethodStr "+methodName + " "+args ;
}
// Corresponds to pluginLoader.callMethodData
public func callMethodData(_ methodName: String, _ data: Data) -> String {
let a = String(data: data, encoding: .utf8)
return " callMethodData " + methodName + " "+(a ?? "");
}
// Corresponds to pluginLoader.callMethodReturnData
public func callMethodReturnData(_ methodName: String, _ data: String) -> Data {
let a = "callMethodDataReturnData "+methodName;
return a.data(using: .utf8)!;
}
// Called when script ends to release resources
public func disposed() {
print("disposed plugin...")
}
// Constructor used when instantiating
public required override init(){
}
}

Package Plugin

Xcode Build

  • Use Product menu in Xcode to build the plugin
    pluginhost
  • Click Build to compile. Click Show Build Folder in Finder, go to the build output directory, and copy out the .framework file
    pluginhost

Command-Line Build

  • Pre-written script; replace helloworldplugin with your plugin name. Output is in the current project directory after build
#!/bin/bash
WORK_DIR= `pwd`
echo `$WORK_DIR`
build_plugin_frame(){
rm -rf /tmp/derivedDataPath/*
xcodebuild build -project helloworldplugin.xcodeproj -scheme helloworldplugin -sdk iphoneos -configuration Release -derivedDataPath /tmp/derivedDataPath -allowProvisioningUpdates
cp -r /tmp/derivedDataPath/Build/Products/Release-iphoneos/helloworldplugin.framework ./
cd $WORK_DIR
}
build_plugin_frame

Use Plugin

  • For development
  • In EC iOS dev plugin, create a project, build a debug package, choose Add Third-Party Plugin, and select the framework file pluginhost
  • After packaging, sign the IPA, install on phone, and debug code
  • For release
  • Same steps to select the plugin file. When publishing, choose Standard Package or Enterprise Package