Extending PhoneGap to Return the iPhone’s Unique Identifier
22 Aug
Yesterday I wrote about the nascent project, PhoneGap, that acts as a Cocoa Touch-native wrapper to your web-based application. I’ve been playing with it non-stop since I stumbled accross it and it works like a charm!
When phonegap initializes, it loads a few parameters from the client iPhone into your javascript environment. By default these include the phone model, version, and gap version. For my specific application, I wanted to load the iPhone’s unique identifier.
To add this uniqueIdentifier parameter is simple – looking in the phonegap sources, Find the GlassAppDelegate.m script in the Classes folder. Using Xcode’s method-finder bar, navigate to the (void)webViewDidStartLoad:(UIWebView *) method and replace it with the following:
- (void)webViewDidStartLoad:(UIWebView *)webViewLocal {
NSLog(@"Page loaded");
NSString *jsCallBack = nil;
jsCallBack = [[NSString alloc] initWithFormat:@"\
__gap = true; \
__gap_version='0.1'; \
__gap_device_model='%s'; \
__gap_device_version='%s'; \
__gap_device_uniqueid='%s';",
[[[UIDevice currentDevice] model] UTF8String],
[[[UIDevice currentDevice] systemVersion] UTF8String],
[[[UIDevice currentDevice] uniqueIdentifier] UTF8String]
];
//NSLog(jsCallBack);
[webViewLocal stringByEvaluatingJavaScriptFromString:jsCallBack];
[jsCallBack release];
}
I’m simply access the uniqueIdentifier property of Cocoa’s UIDevice class.
To be able to access this paramter in your javascript, jump into the gap.js that you should be loading in your iphone web app, find the Device.init function, and replace with:
init: function(model, version) {
try {
Device.available = __gap;
Device.model = __gap_device_model;
Device.version = __gap_device_version;
Device.gapVersion = __gap_version;
Device.uniqueIdentifier = __gap_device_uniqueid;
} catch(e) {
alert("GAP is not supported!")
}
},
Voila! You can now access the client iphone’s unique identifier via Device.uniqueIdentifier in your javascript.
Next up – figuring out how to access the keyboard through the UIWebView :X
