LoginSignup
0
1

More than 3 years have passed since last update.

Swift WebKitを持つViewControllerへの遷移

Last updated at Posted at 2020-10-03

ある条件(例えばボタンを押したら)で遷移

@IBAction func btnClick(_ sender: Any) {
    self.performSegue(withIdentifier: "segueで指定したIdentify", sender: nil)
}

指定したURLをWebKitで開く


@IBOutlet weak var webPage: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let homeUrl="https://google.com"
        openUrl(urlString: homeUrl)
        webPage.navigationDelegate = self
    }

    // 文字列で指定されたURLをWeb Viewを開く
        func openUrl(urlString: String) {
            let url = URL(string: urlString)
            let request = NSURLRequest(url: url!)
            webPage.load(request as URLRequest)
        }

前のページに戻るボタンのアクション

@IBAction func goBack(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

WebKitのフルスクリーン

Segueの下記の2つを設定する
スクリーンショット 2020-10-04 22.47.56.png

iPhoneX等の場合、上記だけだと画面上の時刻などが出っぱなしになるのでコレも必要
スクリーンショット 2020-10-05 13.29.17.png

デバイスの向きを得る

デバイスのモーションセンサを得るためには下記の設定が必要

    var rcvURL=""
    override func viewDidLoad() {
        super.viewDidLoad()
        webView.uiDelegate = self //これが必要
        webView.navigationDelegate = self
        openUrl(urlString: rcvURL)
    }

MediaPlaerPlaybackのエラー?

画面にMediaPlayerの白い箱と動画再生のコントローラが出る。Web Viewのプロパティ(?)にすべてチェック入れるとこの問題が解決できた。原因不明。
スクリーンショット 2020-10-05 16.29.03.png


参考
https://fuuno.net/swift/bo_bi_1/bo_bi_1.html
https://yuu.1000quu.com/how_to_use_segue
https://qiita.com/zlia_7/items/e99b77372d8c0f38d98b
https://qiita.com/Sab_swiftlin/items/4f184c66477e291d4a0a

0
1
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
0
1