Swift - 将 Google AdMob 与我的应用程序集成时遇到问题

Swift - trouble integrating Google AdMob with my application


我正在尝试使用 Google 的 AdMob SDK 为我的 iOS 应用程序实施广告。

我一直在关注官方 [docs](https://developers.google.com/admob/ios/quick-start),但我无法让它正常工作。
到目前为止我做了什么:
  1. 创建了一个 AdMob 帐户并注册了我的应用程序。
  2. 更新了我的 info.plist 使其与教程中显示的格式匹配。
    不用说,我用 ID 替换了教程的应用程序 ID我从我的 Google AdMob 帐户获得。
  3. 使用 info.plist 更改(没有 任何导入或附加代码)按原样构建我的应用程序已经引入了一些奇怪的日志:
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. 然后,我使用 cocoapods 下载了 Google-Mobile-Ads-SDK,并尝试使用以下代码初始化移动广告 SDK:
    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
    }
    ...

这引入了一个新错误:主线程检查器:UI API 在后台线程上调用 (除了来自 #3 的奇怪日志记录剩下的)。

我的问题是什么?如有任何帮助,我们将不胜感激!

我最终使用旧版本的 SDK 解决了我的问题,版本 7.43.0 对我有用。
为了解决UIAPI的问题,我也不得不使用DispatchQueue.main.async
ViewController.swift

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

我将 createAndLoadInterstitial 写在一个单独的文件中:
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()
    }
}

希望未来的 Google 员工能从中受益:)