Cordova Plugin Support
Crosswalk for iOS provides support for Cordova Plugins by introducing the Cordova.framework based on the Crosswalk Extension mechanism. The Cordova extension is responsible for managing the life cycle of the Cordova Plugins, replicates the environment that Cordova Plugins run in, and provides the functionalities that Cordova Plugins need based on the Crosswalk Extension framework. You can leverage the existing Cordova Plugins to extend Crosswalk's functionalities without changing any code of the plugins.
Cordova Plugin API
We are going to support all the Cordova Plugin API in the coming future, the status of API availability is:
Supported
- org.apache.cordova.battery-status
- org.apache.cordova.console
- org.apache.cordova.contacts
- org.apache.cordova.device
- org.apache.cordova.device-motion
- org.apache.cordova.device-orientation
- org.apache.cordova.dialogs
- org.apache.cordova.file
- org.apache.cordova.file-transfer
- org.apache.cordova.geolocation
- org.apache.cordova.globalization
- org.apache.cordova.inappbrowser
- org.apache.cordova.media
- org.apache.cordova.media-capture
- org.apache.cordova.network-information
- org.apache.cordova.vibration
- org.apache.cordova.splashscreen
- org.apache.cordova.statusbar
Under Development
You are warmly welcome to contribute together with us to enable more Cordova Plugins for Crosswalk.
Quickstart
In this section we'd like to show you a simple demo application with org.apache.cordova.device embedded to demonstrate the processes to create an application with Cordova Plugin support.
Setup DeviceDemo Project
Create DeviceDemo Project
We need to create an application project to host the Cordova plugin and the web resources.
Create
DeviceDemoProjectOpen Xcode. Create an iOS application with the "Single View Application" template with project name: "DeviceDemo". Select "Swift" as project language.
For quick demo, replace the auto-generated
ViewController.swiftwith the corresponding file in crosswalk-ios/AppShell/AppShell/CordovaViewController.swift, which has setup a XWalkView instance for you already.Just clear the contents of the auto-generated
ViewController.swiftfile, replace them with: crosswalk-ios/AppShell/AppShell/CordovaViewController.swift.
Use CocoaPods to integrate the crosswalk-ios and crosswalk-extension-cordova into the project.
- In the
DeviceDemodirectory, create a file calledPodfile:> cd DeviceDemo > touch Podfile
With the contents as below:
platform :ios, '8.1' use_frameworks! pod 'crosswalk-extension-cordova', '~> 1.1'This will tell CocoaPods that the deploy target is iOS 8.1+ and to integrate library
crosswalk-extension-cordovawith the latest version of1.1.x. Remember to adduse_frameworks!which is becausecrosswalk-iosis partly written in Swift and it has to be built as a framework instead of a static library.You may notice that we haven't added the dependency of
crosswalk-ios. This is not necessary becausecrosswalk-extension-cordovadepends oncrosswalk-iosand the dependency is automatically handled by CocoaPods.- Install the
Podtargets into the project.
Quit the Xcode firstly, then in the
DeviceDemodirectory, use the following command to install pods:> pod installThen you'll find a
DeviceDemo.xcworkspaceis generated, and CocoaPods will notify you to use this workspace instead of theDeviceDemo.xcodeprojfrom now on.- In the
Open the
DeviceDemo.xcworkspace, try to build theDeviceDemoapplication target, to see if it works fine.
Import org.apache.cordova.device Plugin Resources
Next step is to import both native and html5 resources from org.apache.cordova.device into the project.
Clone org.apache.cordova.device
> git clone https://github.com/apache/cordova-plugin-device.gitAdd native part of the plugin source file in to the application
Select
DeviceDemoproject, inFile->Add Files to "DeviceDemo"...,add
CDVDevice.handCDVDevice.mfromcordova-plugin-device/src/iosinto the project.Notice: Xcode will notify you if you need to create the bridging header, just create it.
Add HTML5 part of resources into the application
> cd DeviceDemo; mkdir -p www/plugins/org.apache.cordova.device/www/ > cd www; > cp ../../cordova-plugin-device/www/device.js \ plugins/org.apache.cordova.device/www > vim plugins/org.apache.cordova.device/www/device.jsAs the
device.jsonly provides the content of implementation of thedeviceobject, we need to wrap it into a cordova module before importing it into the web app's context. (We will provide a script to make this step automatic.)cordova.define("org.apache.cordova.device.device", function(require, exports, module) { // The original content of device.js goes here. ... });Create the configuration needed by Cordova Extension
We also need to add a configuration file to tell Cordova Extension how to load the Cordova Plugin. Here we simulated the way that Cordova project does, in order to provide the same behavior as the Cordova project. In
wwwdirectory:> touch cordova_plugins.js > vim cordova_plugins.jsWith the content:
cordova.define('cordova/plugin_list', function(require, exports, module) { module.exports = [ { "file": "plugins/org.apache.cordova.device/www/device.js", "id": "org.apache.cordova.device.device", "clobbers": [ "device" ] } ]; module.exports.metadata = // TOP OF METADATA { "org.apache.cordova.device": "0.2.12" } // BOTTOM OF METADATA });
Create The Web Apps
After importing the device plugin, we need to create the web application part to leverage the ability of the device plugin.
Create
index.htmlinwwwdirectory.> touch index.html > vim index.htmlWith the content:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 5px; } </style> <title>Cordova device API plugin demo</title> </head> <body> <p>This is the demo page for testing org.cordova.plugin.device.</p> <table id='table' style='width:100%'> <caption>Device Properties</caption> <thead> <tr> <th>Name</th> <th>Value</th> </tr> </thead> </table> <script> document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var table = document.getElementById('table'); for (var propName in device) {; if (typeof(device[propName]) == 'function') { continue; } var row = document.createElement('tr'); var name = document.createElement('td'); var value = document.createElement('td'); name.innerHTML = propName; value.innerHTML = device[propName]; row.appendChild(name); row.appendChild(value); table.appendChild(row); } } </script> </body> </html>In this demo we will listen to the
devicereadyevent and retrieve the system information from thedeviceobject, and then add them into a table. Thedeviceobject is provided byorg.apache.cordova.deviceplugin and the system information is gathered from native side.Embed the web resources into the project
We need to add the web resources(including plugin and app resources) into the project and package as a native iOS application.
Select
DeviceDemoproject, inFile->Add Files to "DeviceDemo"...,Add
wwwdirectory into the project, withCreate folder referencesoption selected.
Create the
manifest.plistfor the projectThe
manifest.plistdescribes the extension info embedding with the application, as well as the Cordova Plugin info with it.Select
DeviceDemoproject, inFile->New->File,Choose
iOS->Resource->Property List, and enter the name:manifest.
Then add:
start_urlwith String type andindex.htmlas value.xwalk_extensionswith Array type, and add:xwalk.cordovaas an entry.
cordova_pluginswith Array type and add an item of Dictionary type within it. In the dictionary type add:classfor key andCDVDevicefor value;namefor key andDevicefor value;
Build and run the
DeviceDemoapplication target oniOS Deviceto see if it works fine.Now the demo should be ready to use. That's it.
Demos
In the demos directory of ios-extension-crosswalk project, there are two demos to demonstrate the usage of Cordova plugin support. You may try them out to see how it works.
English
