- Let native iOS developers take advantage of some of the great web technologies inside WebKit.
Typically, an iOS application will have one GAScriptEngine instance. You can keep the instance in a globally accessible place, such as the UIApplication delegate, or keep it with the object/view that is managing the hidden UIWebView.
This category adds accessors to access the "document" and "window" objects of the HTML document loaded in the WebView. You can then use any GAScriptObject functionality on them.
[NSObject valueForKey:] and
[NSObject setValue:forKey:], as you would with other Objective-C classes. Only non-function properties are exposed via KVC.
GAScriptObject handles marshaling of data between the languages. It will handle quoting strings, passing date values, and dealing with sub-objects and arrays.
There are three "callFunction" methods that can be used to call a Function on the object with either no arguments, one argument, or an array of arguments. All the data types supported by the KVC code are supported as function arguments and return types.
GAScriptObject implements NSFastEnumeration so that you can write loops that iterate over the script object's property names and values.
There are unit tests in the /Tests folder that show how to use various features (and they make sure the features work!).
A simple way to get started is to:
- Create a hidden UIWebView. It can be parented to the app's UIWindow, or a view in a UIViewController.
- Create a GAScriptEngine and pass the UIWebView to
- Now you can access the "document" or "window" object via the GAScriptEngine instance, or create your own objects using
You can access sub-objects or call functions using the returned GAScriptObject.
// Get the "document" object from the script engine id document = [scriptEngine documentObject]; // Get a GAScriptObject that represents the DOM element with id="myelement" id myElement = [document getElementById:@"myelement"]; // The above is the same as this, but nicer to read and write id myElement2 = [document callFunction:@"getElementById" withObject:@"myelement"];