我已经为Cocoa Touch Framework创建了示例应用程序,并且已经通过带有示例网址的WKWebView。我在其中一个应用程序中使用了该框架,并尝试从主类中打开wkwebview。下面是我的可可触摸框架代码。它始终显示我无法发出信号服务com.apple.WebKit.Networking:113:在控制台中找不到指定服务此消息。
import UIKit
import WebKit
public class ConnectedAppController: UIViewController, WKNavigationDelegate {
var webView : WKWebView!
override public func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
public func opensampleWebView() {
let myBlog = "https://www.google.com/"
let url = NSURL(string: myBlog)
let request = URLRequest(url: url! as URL)
// init and load request in webview.
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
self.view.addSubview(webView)
webView.load(request)
}
private func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
}
public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Strat to load")
}
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("finish to load")
}
答案 0 :(得分:0)
您的问题并不明显,但我怀疑您是在另一个班级的opensampleWebView
来电中致电prepareForSegue
。
虽然您在self.view.addSubview(webView)
之前正确地呼叫webView.load(request)
,但我怀疑您尝试添加到尚未加载的视图。
如果您在opensampleWebView()
中致电viewDidLoad
,有什么不同吗?
override public func viewDidLoad() {
super.viewDidLoad()
opensampleWebView()
}
答案 1 :(得分:0)
您需要覆盖 loadView()
功能Swift 4
public class ConnectedAppController: UIViewController, WKNavigationDelegate {
var webView : WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
self.webView.allowsBackForwardNavigationGestures = true
let myBlog = "https://www.google.com/"
if let url = URL(string: myBlog) {
self.webView.load(URLRequest(url: url))
}
}
}