I am making an app in IOS with phonegap/cordova 2.1.0 framework. I want to invoke a javascript function residing inside index.html from objective-c. So, first i am loading the index.html and then trying to call the javascript function. My code is as under:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *myFile = [path stringByAppendingPathComponent:@"index.html"];
//NSLog(@"base url= %@",myFile);
NSData *myFileData = [[NSData alloc] initWithContentsOfFile:myFile];
NSString* myFileHtml = [[NSString alloc] initWithData:myFileData encoding:NSASCIIStringEncoding];
[theWebView loadHTMLString:myFileHtml baseURL:baseURL];
// to call javascript function
[theWebView stringByEvaluatingJavaScriptFromString:@"onDeviceReady()"];
The second last line loads the index.html and the last line calls the function inside it. But, somehow the above code is not working. Am i going wrong somewhere syntax wise.
Alos, since the index.html is already being loaded before, so how can i specify the file name(ie. index.html) where the function exists without loading it once again? Thanks.
Javascript function is as follows:
function onDeviceReady(callback) {
var item = window.localStorage.getItem("key");
var message = "deviceready localStorage.getItem('key')="+item+" localStorage.length="+window.localStorage.length;
if( item == null){
item = "";
}
item = item + "value";
window.localStorage.setItem("key", item);
var value = window.localStorage.getItem("key");
if ( deviceType == 'android' && gAppControl.loadScript == false ){
loadScript('iscroll.js');
gAppControl.loadScript = true;
document.addEventListener("backButton", backPressed, false);
}
var db = window.openDatabase(dbName, "1.0", gAppConfig.dbMessage, 200000);
db.transaction(queryDB, errorCB , callback);
// return
return;
}
onDeviceReady()
this is the phonegap API, if the device is not ready, you will fail to call any Phonegap plugin, so What's the detail in the onDeviceReady() function? any phonegap-plugin involved?
The code seems no problem, and here is my code , with a bridge to comunicate with Objective-C and HTML-Javascript,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.