others-how to solve 'Cannot find type 'GADBannerView' in scope' error when trying to add Google AdMob's GADBannerView to our app ?

1. Purpose

In this post, I would demo how to solve this error when trying to add GADBannerView from Google AdMob to our app:

Cannot find type 'GADBannerView' in scope

2. Environment

  • Mac OS 10.15
  • Swift 5
  • Xcode 12

3. The solution

3.1 Original code

import UIKit

class CategoryTableViewController: UITableViewController {
    @IBOutlet weak var adBanner: GADBannerView!
    ....
}

The above code is trying to add the admob view to our view controller.

GADBannerView is a view that shows Google Admob advertisements, it’s included in the GoogleMobileAds framework.

A view that displays banner ads. See https://developers.google.com/admob/ios/banner to get started.

After building, we found this error shown in our xcode editor:

Cannot find type ‘GADBannerView’ in scope

3.2 The solution

  1. You should open your project by clicking the xxx.xcworkspace in your project’s root folder, because we are using cocoapods to manage our dependencies.
  2. Clean and build your project again
  3. Change the code as follows:
import UIKit
import GoogleMobileAds //key point

class CategoryTableViewController: UITableViewController {
    @IBOutlet weak var adBanner: GADBannerView!
    ...
}

Now it works!

4. Summary

In this post, I demonstrated how to solve the Cannot find type ‘GADBannerView’ in scope error when trying to add admob to our project.