KGJavascriptBridge
Description
The purpose of KGJavascriptBridge is to allow a link in the HTML content of a page to trigger a function of the app native code, optionally passing in parameters. A common implementation is to interact with device hardware like the camera and to interact with system level resources such as calendars, contacts and stored media.KGJavascriptBridge
is a singleton class which is instantiated inside thedocument:didExecuteCommand:
delegate method of KGPagedDocControl
. For the module to work correctly, there are three items that must be named the same.
- The name of the category you wish to use.
- The name of a method in that category that receives an NSDictionary of the optional query parameters sent in the URL.
- The host of the URL in the content that the user will tap to trigger the bridge.
For example, the category KGJavascriptBridge+addToCalendar
implements a method -(void)addToCalendar:(NSDictionary *)parameters andPageControl:(KGPagedDocControl *)pageControl
which is triggered by a URL in the form pugpig://addtoCalendar?....
.
Installation
To install:
gitsubtreeadd-PKGAnalyticsgit@bitbucket.org:kaldorgroup/kaldor-kgajavascriptbridge-iosmodule.gitmaster#ifusingssh gitsubtreeadd-PKGAnalyticshttps://yourusername@bitbucket.org:kaldorgroup/kaldor-kgajavascriptbridge-iosmodule.gitmaster#ifusinghttps
To update:
gitsubtreepull-PKGAnalyticsgit@bitbucket.org:kaldorgroup/kaldor-kgajavascriptbridge-iosmodule.gitmaster#ifusingssh gitsubtreepull-PKGAnalyticshttps://yourusername@bitbucket.org:kaldorgroup/kaldor-kgajavascriptbridge-iosmodule.gitmaster#ifusinghttps
Import the KGJavascriptBridgeModule.h
file into the class.
This module is created for use in projects that manage memory using ARC. To use in a non-ARC project you may need to change references to __block
to __weak
.
Using KGJavascriptBridge
To create a link between the Javascript in the HTML content and the Obj-C code of the app, you need two things.
- A correctly formed URL to trigger the
document:didExecuteCommand:
method. REQUIRED. - A correctly named javascript function that accepts the relevant item for the selected usage. OPTIONAL
The URL will be composed of a custom scheme, a host whose name must match the name of the category you're intending to handle the bridge. You can optionally append query elements to provide parameters. An example url for the cameraCapture category is shown below:
pugpig://cameraCapture?cameraSource=front&scriptString=functionName
The scheme of pugpig://
ensures the URL will be handled by the correct method of the KGPagedDocControl. The host of cameraCapture will be used by the main KGJavascriptBridge class to determine which category to use and which method to call. The query parameters specify the camera to use and the name of the function to call on completion. A complete description of the options available for the cameraCapture category is provided below.
In the document:didExecuteCommand:
method, filter the code by the scheme, an in the conditional branch for the type you want to use the bridge for instantiate the KGJavascriptBridge object by passing the URL sent into the method and the KGPagedDocControl object to the sharedInstance:
[[KGJavascriptBridge sharedInstance] pageControl:self.pageControl didExecuteCommand:url]
Comments
0 comments
Please sign in to leave a comment.