1

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) ^~~~~~~~~~~~~~~

5
  • If it happens on that line then bannerView is the only thing that can be nil. Commented Jul 28, 2017 at 18:44
  • @Scott B. You should use guard let to handle nil values so that app doesn't crashes and you can debug reason. Commented Jul 28, 2017 at 19:01
  • Do you use cocoapods for admob? Commented Jul 28, 2017 at 21:07
  • If you use cocoapods, check your podfile if it includes use_frameworks!. If it's not included, add it and run pod install again. After that clean, build and run your project Commented Jul 28, 2017 at 21:14
  • Nope, I didn’t use cocoapods, I just dropped the google ads framework into my project and made sure copy items if needed was checked Commented Jul 28, 2017 at 21:51

2 Answers 2

1

I had a similar issue, and was able to resolve it. This might help someone...

My problem was that I had not set an IBOutlet Variable, and instead had

var bannerView: GADBannerView! 

but instead I needed to attach my view from my storyboard to my ViewController with an IBOutlet

@IBOutlet var bannerView: GADBannerView! 

I'd make sure that its linked correctly.

Along with that I removed the code that was programmatically adding the bannerView to the story board. I think this also causes some issues if you mix the storyboard, and programmatically adding it

Sign up to request clarification or add additional context in comments.

Comments

1

Most likely you didn't connect the 'bannerView' IBOutlet variable and the relevant storyboard view.

3 Comments

I reconnected it and the same thing still happens, so that's not the problem
Are you sure that in the storyboard your view is an instance of the GADBannerView class?
Yes, I'm sure it is as it says it in the identity inspector

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.