【问题标题】:WKWebView gives SecurityError when bundling html and javascript with app将 html 和 javascript 与应用程序捆绑在一起时,WKWebView 会出现 SecurityError
【发布时间】:2014-09-05 11:47:12
【问题描述】:

我们正在尝试将混合应用程序从 UIWebView (iOS DOM WebDatabase API(即“web sql 数据库”)存储内容时遇到 SecurityErrors。

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

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

同样的代码也适用于 UIWebView。出于某种原因,我可以回退到使用本地存储,但是使用 WebSQL 数据库是不行的。我只能推测这与同源政策或相关的东西有关。

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

关于我如何解决这个问题的任何线索?在 WKWebView 上设置任何选项来修复它?

这是我们加载网络相关内容的方式:

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

html 文件只是加载一个具有相对路径“myCode.js”的 javascript 文件。

【问题讨论】:

    标签: ios dom uiwebview web-sql wkwebview


    【解决方案1】:

    iOS 8.0(我认为是 8.1 B1)中的 WKWebView 存在一个问题 (OpenRadar),阻止它加载本地文件。它也可能会影响本地存储。详情请见this question

    【讨论】:

    【解决方案2】:

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

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

    它为所有数据库提供 50MB 的配额,而不是允许打开它们的默认值 0。这种行为没有记录在案,所以我不知道 Apple 对此持何立场。

    此外,这个问题似乎将在 iOS 10 中得到修复。

    【讨论】:

      【解决方案3】:

      我制作了一个“插件”,允许您在 WKWebView 中使用 WebSQL(更多是它的实现)。可以在这里找到

      https://github.com/ajwhiteway/WKWebSQL

      import WKWebSQL
      .
      .
      .
      var webView = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
      WKWebSQL.LoadPlugin(webView)
      

      将其加载到页面中。目前并不真正支持版本控制。随意添加。

      【讨论】:

        猜你喜欢
        • 2011-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-16
        • 1970-01-01
        • 2019-08-16
        • 1970-01-01
        相关资源
        最近更新 更多