Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)")))
    }
}
like image 317
Patrik Rikama-Hinnenberg Avatar asked May 05 '18 13:05

Patrik Rikama-Hinnenberg


People also ask

Why is WKWebView not opening links with target _blank?

The culprit is HTML attribute target set to value _blank which is used to signal links that should be opened in new tab instead of the current one. This target setting is meant to tell the browser (which is WKWebView in this case) that this link should be open in new tab by default. Which is the root of the problem.

What is the difference between UIWebview and WKWebView?

Difference Between UIWebview and WKWebView UIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.

How do I load a URL in WKWebView swift 5?

Loading local content WKWebView can load any HTML stored in your app bundle using its loadFileURL() method. You should provide this with a URL to some HTML file that you know is in your bundle, along with another URL that stores any other files you want to allow the web view to read. That url.


2 Answers

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

like image 63
Jogendar Choudhary Avatar answered Oct 25 '22 16:10

Jogendar Choudhary


In my case, I was trying to load an HTTP site instead of HTTPS. Initially it didn't load anything but when I used HTTPS, the site loaded as expected.

like image 35
Bilbo Baggins Avatar answered Oct 25 '22 18:10

Bilbo Baggins