Search code examples
iosswiftadmobappdelegategoogle-mobile-ads

Swift - trouble integrating Google AdMob with my application



I'm attempting to implement ads for my iOS application using Google's AdMob SDK.

I've been following the official [docs](https://developers.google.com/admob/ios/quick-start), but I'm having trouble getting that to work.
What I've done so far:
  1. Created an AdMob account and registered my app.
  2. Updated my info.plist so it matches the format shown in the tutorial.
    Needless to say, I replaced the tutorial's app ID with the ID I got from my Google AdMob account.
  3. Building my application as-is with the info.plist changes (without any imports or additional code) already introduced some weird logs:
2020-11-16 03:14:08.242623+0200  [5520:1418287]  - <Google>[I-ACS025031] AdMob App ID changed. Original, new: (nil), ca-app-pub-2838133095156647~5679250242
2020-11-16 03:14:08.249700+0200  [5520:1418287] [NetworkInfo] Could not successfully update network info for descriptor <CTServiceDescriptor 0x281b3cec0, domain=1, instance=2> during initialization.
2020-11-16 03:14:08.250665+0200  [5520:1418287] [NetworkInfo] Signal strength query returned error: Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied", descriptor: <CTServiceDescriptor 0x281b3f1a0, domain=1, instance=1>
2020-11-16 03:14:08.251144+0200  [5520:1418287] [NetworkInfo] Signal strength query returned error: Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied", descriptor: <CTServiceDescriptor 0x281b3cec0, domain=1, instance=2>
2020-11-16 03:14:08.258569+0200  [5520:1418287]  - <Google>[I-ACS023007] Analytics v.70100000 started
2020-11-16 03:14:08.259511+0200  [5520:1418287]  - <Google>[I-ACS023008] To enable debug logging set the following application argument: -APMAnalyticsDebugEnabled (see https://help.apple.com/xcode/mac/8.0/#/dev3ec8a1cb4)
2020-11-16 03:14:08.285264+0200  [5520:1418283] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2020-11-16 03:14:08.286833+0200  [5520:1418283] [MC] Reading from public effective user settings.
2020-11-16 03:14:08.303409+0200  [5520:1418283]  - <Google>[I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
2020-11-16 03:14:08.325373+0200  [5520:1418286]  - <Google>[I-ACS023012] Analytics collection enabled
  1. Then, I downloaded Google-Mobile-Ads-SDK using cocoapods & attempted to initialize the mobile ads SDK with the following code:
    AppDelegate.swift:
// other imports
import GoogleMobileAds


@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window : UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
        GADMobileAds.sharedInstance().start(completionHandler: nil)
        if #available(iOS 13, *) {
            return true
        }
        else {
            self.window = UIWindow()
            let vc = ViewController()
            self.window!.rootViewController = vc
            self.window!.makeKeyAndVisible()
        }
        return true
    }
    ...

This introduces a new error: Main Thread Checker: UI API called on a background thread (in addition to the weird loggings from #3 that remain).

What is my problem? Any help would be appreciated!


Solution

  • I ended up solving my issue by using an older version of the SDK, version 7.43.0 worked for me.
    In order to solve the UI API problem, I also had to use DispatchQueue.main.async:
    ViewController.swift

    override func viewDidLoad() {
       DispatchQueue.main.async {
           self.interstitial = self.createAndLoadInterstitial()
       }
       ...
    

    I wrote the createAndLoadInterstitial on a separate file:
    AdMob.swift

    import GoogleMobileAds
    
    
    extension ViewController: GADInterstitialDelegate {
        func createAndLoadInterstitial() -> GADInterstitial {
            self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")  // Google's test interstitial adUnitID
            interstitial.delegate = self
            interstitial.load(GADRequest())
            return interstitial
        }
    
        func interstitialDidDismissScreen(_ ad: GADInterstitial) {
            // on interstitial dismiss - load another one
            self.interstitial = createAndLoadInterstitial()
        }
    }
    

    Hopefully future Googlers find this helpful :)