3
9

More than 5 years have passed since last update.

WKWebViewでwebページを表示するiOSアプリを作る

Last updated at Posted at 2016-12-27

webアプリをアドレスバー無しで表示させてネイティブアプリっぽくする方法を探してwebアプリモードなど試してみたが動きが重すぎるので、ネイティブアプリでweb表示だけさせてみる。

とりあえず、Appleのリファレンスから以下をコピペ

ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {

    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string: "https://www.apple.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }}

これだけでappleの公式は表示された。
が、urlを書き換えると表示されなくなるため、調べてみたら
どうやらhttpsでなくhttpによる通信はデフォルトで許可されていないらしい。

info.plistのAppTransportSecurityをtrueにするとか書いてあるが、
GUIで開くと見当たらないので、info.plistをエディタで開いて以下を追記

info.plist
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
3
9
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
3
9