5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】WKWebViewでローカルファイルを読み込み、Ajaxを利用する際の設定

5
Last updated at Posted at 2019-09-30
import UIKit
import WebKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let config = WKWebViewConfiguration()
        config.setValue(true, forKey: "allowUniversalAccessFromFileURLs") //こいつが重要
        let webView = WKWebView(frame: view.bounds, configuration: config)
        view.addSubview(webView)
        guard let localHTMLUrl = Bundle.main.url(forResource: "index", withExtension: "html") else { return }
        webView.loadFileURL(localHTMLUrl, allowingReadAccessTo: localHTMLUrl)
    }
}

外部APIの通信が失敗していたところ"allowUniversalAccessFromFileURLs"をconfigにセットしてやると解決した。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?