【问题标题】:Swift - trouble integrating Google AdMob with my applicationSwift - 将 Google AdMob 与我的应用程序集成时遇到问题
【发布时间】:2020-11-16 01:33:19
【问题描述】:

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

我一直在关注官方 [docs](https://developers.google.com/admob/ios/quick-start),但我无法让它发挥作用。
到目前为止我所做的:
  1. 创建了一个 AdMob 帐户并注册了我的应用。
  2. 更新了我的 info.plist,使其与教程中显示的格式相匹配。
    不用说,我将教程的应用 ID 替换为从 Google AdMob 帐户获得的 ID。
  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 的奇怪日志记录之外)。

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

【问题讨论】:

    标签: ios swift admob appdelegate googlemobileads


    【解决方案1】:

    我最终通过使用旧版本的 SDK 解决了我的问题,版本 7.43.0 对我有用。
    为了解决 UI API 问题,我还不得不使用 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 员工会觉得这很有帮助 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-12
      • 2019-11-25
      • 2022-12-10
      • 1970-01-01
      • 2016-10-24
      • 2017-08-28
      • 1970-01-01
      • 2019-05-21
      相关资源
      最近更新 更多