3

I'm writing a webkit-based osx application with a bundle of javascript files. I setup the webView like that:

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]];

Here is a part of the code of data/test.html.the alert function is hooked to NSLog the message.

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;
}

Here is the log:

startup

local storage works

hello world; local storage works!

local database works

CONSOLELOG:17 @ SECURITY_ERR: DOM Exception 18: An attempt was made to break through the security policy of the user agent. @ file:///path/of/my.app/Contents/Resources/data/test.html

I don't know why window.openDatabase works but cannot create a database. Thank you.

1 Answers1

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.

Community
  • 1
  • 1
LDK
  • 2,555
  • 5
  • 27
  • 40