webView变量为nil。
致命错误:在解包可选值时意外发现nil
如何使用单例对象?
class PushHandler: NSObject, UAPushNotificationDelegate {
func receivedNotificationResponse(_ notificationResponse: UANotificationResponse, completionHandler: @escaping () -> Swift.Void) {
print("The user selected the following action identifier:%@", notificationResponse.actionIdentifier);
if let targetUrl = notificationResponse.notificationContent.notificationInfo["url"] {
let viewController:MCKWebViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MCKWebViewController") as! MCKWebViewController
// .instantiatViewControllerWithIdentifier() returns AnyObject! this must be downcast to utilize it
self.window?.rootViewController?.present(viewController, animated: false, completion: nil)
viewController.goURL(targetUrl as! String)
}
completionHandler()
}
class MCKWebViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
func goURL(_ targetUrl: String) {
let url = URL(string: targetUrl)
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
}
答案 0 :(得分:0)
尝试在_ = viewController.view
方法
instantiateViewController(withIdentifier:)