Extensions
Extensions enable you to extend the ability of the Crosswalk runtime. By creating an extension, you can introduce new objects or functions to the Javascript world, and implement those features in native code, written in either Objective-C or Swift. A Crosswalk Extension consist of:
Native source codes (in Objective-C or Swift):
- An extension class derived from XWalkExtension.
JavaScript wrapper (Optional):
- A JavaScript file which exposes the native code to an app running on Crosswalk.
The Crosswalk for iOS runtime provides a way to auto-generate JavaScript mapping codes and allow you to inject your own JavaScript code for the extension.
How to write an extension
This section shows how to implement an extension.
Create a Cocoa Touch Framework Project
Open Xcode, in "File" -> "New" -> "Project...". Create a "Cocoa Touch Framework" project.
Select the project you've created in Navigator panel, choose "File" -> "Save As Workspace..." to create a workspace for the project.
Use CocoaPods to integrate the
crosswalk-ioslibrary. For the CocoaPods installation and usage, please refer to: CocoaPods.In the project directory, create a file called
Podfile:> touch Podfile;With the contents as below:
platform :ios, '8.1' use_frameworks! pod 'crosswalk-ios', '~> 1.2'Install
Podstarget into the project. Quit the Xcode first, then in the project directory, use command:> pod installAfter the installation, you will find an
<projectName>.xcworkspaceis generated, and CocoaPods output will notify you to use this workspace instead of the<projectName>.xcodeprojfrom now on.Create
extensions.plistto define the mapping information between application JavaScript runtime and the native extension class.In
File->New...->File..., chooseiOS->Resource->Property List. Create a plist file with nameextensions.plistin project directory.Add a new row named
XWalkExtensionswith typeDictionary. The key/value defines like this:
| Field | Type | Content | Example |
|---|---|---|---|
| key | String | The exposed namespace in JavaScript runtime | "xwalk.sample.echo" |
| value | String | The native class name | "EchoExtension" |
Implement The Extension
Create a subclass which derives from
XWalkExtensionwith the name you defined in the extensions.plist. Your subclass can be implemented with either Objective-C or Swift, and Swift is more recommended.Write your own logic in this class.
Crosswalk for iOS provided a most convenient way to generate mappings between native and JavaScript, which means you don't need to write both native and JavaScript logic, then via low level postMessage way with JSON marshalling/unmarshalling support to communicate in between. With the mapping you only need to define the native properties and functions with the prefix that Crosswalk for iOS required, then the JavaScript implementation is automatically generated and inject into the JavaScript world, you can use it directly.
Native to JavaScript Mapping
Method Mapping
Native Method Prefix:
jsfunc_Native Definition:
func jsfunc_<functionName>(cid: UInt32, <params...>) {}JavaScript Mapping:
function <functionName>(<params...>) {}Example:
| World | Definition |
|---|---|
| Native | func jsfunc_echo(cid: UInt32, message: String, callback: UInt32) |
| JavaScript | function echo(message, callback) |
Invoke from JavaScript:
echo.echo("Echo from native:", function(msg) {...})
Property Manpping
Native Method Prefix:
jsprop_Native Definition:
dynamic var jsprop_<propertyName>: <Type>JavaScript Mapping:
<propertyName>Example:
| World | Definition |
|---|---|
| Native | dynamic var jsprop_prefix: String = "" |
| JavaScript | prefix |
Invoke from JavaScript:
var prefix = echo.prefix;echo.prefix = 'HelloWorld';
Constructor Mapping
Native Method Prefix:
noneNative Definition:
initFromJavaScriptJavaScript Mapping:
<Constructor>
Test your extension
Create the test app and workspace.
Embed the XWalkView framework and the extension framework into the app.
Import or create the HTML5 resources.
Load the extension.
English
