当与应用捆绑html和javascript时,WKWebView会给SecurityError [英] WKWebView gives SecurityError when bundling html and javascript with app

查看:155
本文介绍了当与应用捆绑html和javascript时,WKWebView会给SecurityError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试将混合应用从UIWebView(iOS< 8)迁移到WKWebView(iOS 8),但是当尝试使用 DOM WebDatabase API (即'web sql databases')。



以下引发错误如果index.html已从应用程序的捆绑文件加载

  // throws SecurityError:DOM异常18 
var db = openDatabase('mydb','1.0','key value store',1);

与UIWebView相同的代码正常工作。由于某些原因我可以回退使用本地存储,但是使用WebSQL数据库是一个没有进行的事情。我只能推测这与相同的起源政策或相关的东西有关。



有趣的是,从网络加载index.html工作正常: /



有什么线索可以解决这个问题吗?在WKWebView上设置任何修改它的选项?



这是我们如何加载与Web相关的东西:

  NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@indexofType:@html]; 
NSURL * baseURL = [NSURL fileURLWithPath:htmlPath];
NSURLRequest * request = [NSURLRequest requestWithURL:baseURL];

WKWebViewConfiguration * config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:self.myCallbacks name:@NativeApp];

self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:config];

[self.webView loadRequest:request];

html文件只需加载一个具有相对路径myCode.js的javascript文件。 / p>

解决方案

您可以通过将以下方法添加到WKWebView的UIDelegate来解决此问题。



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b b b b b b b b b b b b b b b b b b b b b b b b b b b b $ b currentOriginUsage(unsigned long long)currentOriginUsage
currentDatabaseUsage((unsigned long long)currentUsage
expectedUsage:(unsigned long long)expectedUsage
decisionHandler:(void(^)(unsigned long long newQuota ))decisionHandler {
decisionHandler(1024 * 1024 * 50); //默认为50MB
}

它使所有数据库的配额为50MB,而不是默认为0,允许它们打开。这个行为没有记录,所以我不知道苹果在哪里站在这里。



此外,这个问题似乎会在iOS 10中修复。 >

We are trying to migrate a hybrid app from UIWebView (iOS < 8) to WKWebView (iOS 8), but we are getting SecurityErrors when trying to store stuff using the DOM WebDatabase API (i.e. 'web sql databases').

The following throws an error if the index.html has been loaded from a bundled file with the app

// throws SecurityError: DOM Exception 18
var db = openDatabase('mydb', '1.0', 'key value store', 1);

The same code works fine with UIWebView. I can fallback to using Local Storage for some reason, but using WebSQL databases is a no go. I can only speculate that this has something to do with the same origin policy or something related.

The funny thing is that loading index.html from the network works fine :-/

Any clues as to how I can work around this? Any options to set on the WKWebView that fixes it?

This is how we load the web related stuff:

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];

WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:self.myCallbacks name:@"NativeApp"];

self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:config];

[self.webView loadRequest:request];

The html file simply loads a javascript file that has a relative path, "myCode.js".

解决方案

You can fix this by adding the following method to the UIDelegate of your WKWebView.

- (void)                        _webView:(WKWebView *)webView
    decideDatabaseQuotaForSecurityOrigin:(WKSecurityOrigin *)securityOrigin
                            currentQuota:(unsigned long long)currentQuota
                      currentOriginUsage:(unsigned long long)currentOriginUsage
                    currentDatabaseUsage:(unsigned long long)currentUsage
                           expectedUsage:(unsigned long long)expectedUsage
                         decisionHandler:(void (^)(unsigned long long newQuota))decisionHandler {
    decisionHandler(1024*1024*50); //default to 50MB
}

It gives all databases a quota of 50MB, instead of the default of 0 which allows them to be opened. This behavior isn't documented, so I don't know where Apple stands with this.

Also, it appears this issue will be fixed in iOS 10.

这篇关于当与应用捆绑html和javascript时,WKWebView会给SecurityError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆