Objective c window.openDatabase在基于WebKit的OSX应用程序中抛出安全错误

Objective c window.openDatabase在基于WebKit的OSX应用程序中抛出安全错误,objective-c,macos,webkit,local-database,Objective C,Macos,Webkit,Local Database,我正在编写一个基于webkit的osx应用程序,其中包含一组javascript文件。 我将webView设置为: webview = [[WebView alloc] init]; [webview setPolicyDelegate:self]; [webview setFrameLoadDelegate:self]; [webview setUIDelegate:self]; [webview setResourceLoadDelegate:self]; WebPreferen

我正在编写一个基于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\u ERR:DOM异常18:试图突破用户代理的安全策略。@file:///path/of/my.app/Contents/Resources/data/test.html

我不知道为什么window.openDatabase可以工作,但无法创建数据库。 谢谢。

请看我的答案:

我发布的解决方案对我有效。此外,还有一些与解决方案稍有不同的链接,这些链接可能对您更为有用