I have a WKWebView which loads a URL, and an AdMob banner view is displayed at the bottom of the WKWebView. However, I keep getting the error: 'fatal error: unexpectedly found nil while unwrapping an Optional value' when I run the app.
Here is my code:
@IBOutlet var bannerView: GADBannerView! var webView : WKWebView! var progressView : UIProgressView! override func loadView() { webView = WKWebView() webView.navigationDelegate = self view = webView } override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == "estimatedProgress" { progressView.progress = Float(webView.estimatedProgress) } } override func viewDidLoad() { super.viewDidLoad() let url = URL(string: "https://www.facebook.com")! webView.load(URLRequest(url: url)) webView.allowsBackForwardNavigationGestures = true navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Open", style: .plain, target: self, action: #selector(openTapped)) progressView = UIProgressView(progressViewStyle: .default) progressView.sizeToFit() let progressButton = UIBarButtonItem(customView: progressView) let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload)) toolbarItems = [progressButton, spacer, refresh] navigationController?.isToolbarHidden = false webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil) loadAd() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func loadAd() { bannerView.isHidden = true //error happens at this line bannerView.delegate = self bannerView.adUnitID = "ca-app-pub-..." //id is here bannerView.adSize = kGADAdSizeSmartBannerPortrait bannerView.rootViewController = self bannerView.load(GADRequest()) } func openTapped() { let ac = UIAlertController(title: "Open page…", message: nil, preferredStyle: .actionSheet) ac.addAction(UIAlertAction(title: "apple.com", style: .default, handler: openPage)) ac.addAction(UIAlertAction(title: "hackingwithswift.com", style: .default, handler: openPage)) ac.addAction(UIAlertAction(title: "Cancel", style: .cancel)) present(ac, animated: true) ac.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem } func openPage(action: UIAlertAction) { let url = URL(string: "https://" + action.title!)! webView.load(URLRequest(url: url)) } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { title = webView.title } func adViewDidReceiveAd(_ bannerView: GADBannerView) { bannerView.isHidden = false } func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) { bannerView.isHidden = true } I am unable to figure out what is causing this error - the console provides no detail about what optional value it is referring to. The WKWebView worked fine before I tried to add the banner ad, so I assume it's something to do with it.
Update: this shows up in the console: Printing description of self.bannerView: expression produced error: error: /var/folders/yn/kndnb8ws7bz1r0rc11fnpndw0000gn/T/./lldb/485/expr3.swift:1:99: error: use of undeclared type 'GoogleMobileAds' Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer>(bitPattern: 0x11b5272a0)!.pointee) ^~~~~~~~~~~~~~~
bannerViewis the only thing that can be nil.use_frameworks!. If it's not included, add it and runpod installagain. After that clean, build and run your project