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)")))
}
}
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.
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.
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.
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!))
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With