3

JavaScriptファイルのバンドルを使用して、Webkitベースのosxアプリケーションを作成しています。私はそのようにwebViewをセットアップします:

webview = [[WebView alloc] init];

[webview setPolicyDelegate:self];
[webview setFrameLoadDelegate:self];
[webview setUIDelegate:self];
[webview setResourceLoadDelegate:self];    

WebPreferences* prefs = [webview preferences];
[prefs setUsesPageCache:YES];
[prefs _setLocalStorageDatabasePath:@"/tmp/test"]; // existed folder, writable
[prefs setAllowUniversalAccessFromFileURLs:YES];   // enable cross-domain xmlhttprequest
[prefs setAllowFileAccessFromFileURLs:YES];
[prefs setJavaScriptCanAccessClipboard:YES];

[prefs setDatabasesEnabled:YES];                   // enable localDatabase
[prefs setLocalStorageEnabled:YES];                // enable localStorage
[prefs setDeveloperExtrasEnabled:YES];

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html" inDirectory:@"data"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
[webview.mainFrame loadRequest:[NSURLRequest requestWithURL:fileURL]];

これは data/test.html のコードの一部です。アラート関数はメッセージを NSLog にフックしています。

function test(){
    alert("startup");
    if(window.localStorage){
        alert("local storage works");
    }else{
        alert("local storage not supported");
    }
    localStorage.setItem('testItem', "hello world; local storage works!");
    alert(localStorage.getItem('testItem'));

    if(window.openDatabase){
        alert("local database works");
        window.openDatabase("mydb", "1.0", "my first database", 2 * 1024 * 1024);
    }else{
        alert("local database not supported");
    }
    return true;
}

ログは次のとおりです。

起動

ローカル ストレージの動作

こんにちは世界; ローカルストレージが機能します!

ローカル データベースの動作

CONSOLELOG:17 @ SECURITY_ERR: DOM Exception 18: ユーザー エージェントのセキュリティ ポリシーを突破しようとしました。@ file:///path/of/my.app/Contents/Resources/data/test.html

window.openDatabase が機能する理由はわかりませんが、データベースを作成できません。ありがとうございました。

4

1 に答える 1

0

ここで私の答えを参照してください: https://stackoverflow.com/a/8975014/146099

私が投稿した解決策は私にとってはうまくいきました。より適切に機能する可能性のある、わずかに異なるソリューションへの他のリンクもあります。

于 2012-01-23T16:33:02.210 に答える