Search code examples
swiftwebviewwebkit

Swift 4 WebKit webView does not load URL with added parameter


Just can't find out what is wrong with this webView, it just remains blank no error. If I try without a video id parameter and just "https://www.apple.com" it works. Any ideas?

import UIKit
import WebKit

class VideoPlayerVC: UIViewController, WKUIDelegate {

    @IBOutlet weak var webview2: WKWebView!
    var videoId: String?

    override func viewDidLoad() {
      //test video id, this prints the booty workouts id on youtube!This target also opens on youtube the id on the end of this url: https://www.youtube.com/watch?v=695PN9xaEhs
        if let videoId = videoId {
            print(videoId)
        }

        webview2.load(URLRequest(url: URL(fileURLWithPath: "https://www.youtube.com/watch?v=\(videoId)")))
    }
}

Solution

  • Your id is working fine:

    override func viewDidLoad() {
        super.viewDidLoad()
        let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        self.view.addSubview(webView)
        let url = URL(string: "https://www.youtube.com/watch?v=695PN9xaEhs")
        webView.load(URLRequest(url: url!))
    }
    

    enter image description here