LoginSignup
9
12

More than 3 years have passed since last update.

swiftでUIWebViewを作る(httpにも対応)

Last updated at Posted at 2016-02-22

swiftで簡単なwebviewを作る手順を記載

ソースコード

ViewController.swift
var webview: UIWebView = UIWebView()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webview.frame = self.view.bounds
        self.webview.delegate = self;
        self.view.addSubview(self.webview)

        let url:NSURL = NSURL( string: "http://www.google.co.jp" )!
        let urlRequest: NSURLRequest = NSURLRequest(URL: url)
        self.webview.loadRequest(urlRequest)

    }

で、このままだとurlがhttpの場合は以下のようにセキュリティエラーで怒られます
(IOS9から?)
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

なので、AppTransportSecurityを無効にしてやるか、例外のドメインを追加してやる必要があります。

・AppTransportSecurityを無効にする
1.Info.plistを開く
2.「App Transport Security Settings」を「Dictionary」で追加
3.「Allow Arbitrary Loads」を「Boolean」で追加、valueを「YES」に設定
スクリーンショット 2016-02-23 0.10.40.png

・例外ドメインの追加
1.Info.plistを開く
2.「App Transport Security Settings」を「Dictionary」で追加
3.「Exception Domains」を「Dictionary」で追加
4.「www.google.co.jp」などのドメインを「Dictionary」で追加
5.「NSTemporaryExceptionAllowsInsecureHTTPLoads」を「Boolean」で追加、valueを「YES」に設定
6.「NSIncludesSubdomains」を「Boolean」で追加、valueを「YES」に設定
スクリーンショット 2016-02-22 23.57.17.png

これで表示されます

以上

9
12
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
9
12