我在youtube
加载WKWebView
网址时有一个日志
我在stackOverFlow
中搜索了我的问题的相同标题
但它对我不起作用。
我的代码出了什么问题?
我使用swift4
& xcode9.2
。
感谢。
警告如下:
无法发出服务信号com.apple.WebKit.Networking:113:找不到指定的服务
import UIKit
import WebKit
class DetailViewController: UIViewController {
var videoId: String = ""
var videoTitle: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loadUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadUI() {
view.backgroundColor = UIColor.white
naviSetting()
webViewSetting()
}
func naviSetting() {
self.title = videoTitle
}
func webViewSetting() {
let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let url = URL(string: "https://www.youtube.com/watch?v=\(self.videoId)")
let request = URLRequest(url: url!)
webview.load(request)
self.view.addSubview(webview)
}
}
答案 0 :(得分:1)
我试过编码参数,它对我来说很好。请查看以下代码。
var wkWebView = WKWebView()
override func viewDidLoad()
{
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.wkWebView.translatesAutoresizingMaskIntoConstraints = false
self.wkWebView.frame = CGRect.init(x: 0, y: 0, width: self.wkWebView.frame.size.width, height: self.wkWebView.frame.size.height)
self.view.addSubview(wkWebView)
loadUrl()
}
override func viewWillLayoutSubviews()
{
super.viewWillLayoutSubviews()
self.wkWebView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.wkWebView.contentMode = .scaleAspectFit
}
func loadUrl()
{
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
let url_join = "v=1p38GWfCIhQ"
let myURL = URL(string: "https://www.youtube.com/watch?")
var myRequest = URLRequest(url: myURL!)
myRequest.httpMethod = "POST"
myRequest.httpBody = url_join.data(using: String.Encoding.utf8)
wkWebView.load(myRequest)
}
我希望它对你也有用。 :)
答案 1 :(得分:0)
For me this Code worked fine. Notice, I Used http:// and not https://. But you already have set the settings for http:// to work.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
private let url = URL(string: "http://www.google.com")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let request = URLRequest(url: url!)
webView = WKWebView(frame: self.view.frame)
self.view.addSubview(webView)
webView.navigationDelegate = self
webView.load(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}