phonegap/cordova 2.1.0 フレームワークを使用して IOS でアプリを作成しています。object-c から index.html 内にある JavaScript 関数を呼び出したいです。そのため、最初に index.html をロードしてから、javascript 関数を呼び出そうとしています。私のコードは次のとおりです。
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()"];
最後から 2 行目で index.html をロードし、最後の行でその中の関数を呼び出します。しかし、どういうわけか上記のコードは機能しません。私は構文的にどこか間違っていますか?
また、index.html は以前に読み込まれているので、もう一度読み込まずに関数が存在するファイル名 (つまり index.html) を指定するにはどうすればよいですか? ありがとう。
Javascript関数は次のとおりです。
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;
}