Saturday, June 22, 2013

cordova ios native code logic

For cordova ios project created by cordova create command, the application and plugin starting up includes the following logic:
1.  AppDelegate
In didFinishLaunchingWithOptions, it initializes MainViewController, which is a subclass of CDVViewController. The init of CDVViewController will hook up cordova function with application.

2. CDVViewController
In CDVViewController, __init method, it first registers all application notification event, so that it can relay those event to javascript. Then it calls loadSettings, which will parse config.xml which defines the cordova behavior, including starting page and plugin definition.
The CDVViewController owns all plugins instance after the instances are created. That is the plugin lives within CDVViewController's scope and lifetime. However, it is possible, the same webview loads multiple html js app, in that case, the plugin's state in native code will not be reset automatically when new page loads. For this reason, CDVPlugin interface defines a method onReset to clean up the plugin state from js page to page. This method should be implemented by each plugin.

The CDViewController has an important method createGapView, which is called in viewDidLoad method. This method creates UIWebView control, and also sets CDVViewDelegate to the UIWebView instance, to handle the page event. The createGapView method also hooks up with javascript request using CDVURLProtocol (a sub class of NSUrlProtocol).  When a javascript method is called,

3. plugin
Usually, plugin will not create its instance until its method is called the first time. But it can also create its instance during ApplicationDidFinishingWithOption by setting onLoad attribute in config.xml. If so,  the PluginInitialize method can be implemented to register the application events including openUrl, etc. Some basic event it registered by its base class CDVPlugin interface, so do not register those event again.
The plugin method is alway called from the main thread.




No comments:

Post a Comment