3

我正在编写一个基于 webkit 的 osx 应用程序,其中包含一组 javascript 文件。我像这样设置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

See my answer here: https://stackoverflow.com/a/8975014/146099

The solution I posted worked for me. There are also other links with slightly different solutions that might work better for you.

于 2012-01-23T16:33:02.210 回答