Swift 4 WebKit webView不会加载带有附加参数的URL

时间:2018-05-05 13:16:16

标签: swift webview webkit

无法找出此webView的错误,它只是空白而没有错误。如果我尝试没有视频ID参数,只需" https://www.apple.com"有用。有什么想法吗?

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)")))
    }
}

2 个答案:

答案 0 :(得分:9)

你的身份证工作正常:

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

答案 1 :(得分:1)

就我而言,我试图加载一个HTTP站点而不是HTTPS。 最初它没有加载任何东西,但是当我使用HTTPS时,该站点按预期加载。