在 App Delegate 文件中初始化 Google 移动广告 SDK 的最新正确语法是什么?

What is the most recent correct syntax to initialize the Google Mobile Ads SDK in the App Delegate file?

突然,我在用于初始化 Google 移动广告 SDK 的代码上收到一条警告。 (它在没有任何警告的情况下已经工作了数周,但现在似乎有一种新的代码编写方式。)

这是我的代码:

GADMobileAds.configure(withApplicationID: "ca-app-pub-################~##########")

但它给了我这个警告:'configure(withApplicationID:)' 已弃用:使用 [GADMobileAds.sharedInstance startWithCompletionHandler:]

我试过这样重写它(带和不带方括号):

GADMobileAds.sharedInstance startWithCompletionHandler: "ca-app-pub-################~##########"

但它只是给了我一个错误,它期望 ;.

这个应该怎么写?谢谢!

1) 在Info.plist

中添加以下内容
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-3940256099942544~1458002511</string>

2) 在 AppDelegate

 GADMobileAds.sharedInstance().start(completionHandler: nil)

谢谢 Sr,它在 Xamarin 上对我有用,只是我需要它对代码做一些小改动,如下所示:

GADMobileAds.SharedInstance.Start(completionHandler: null)

在加载广告之前,调用 startWithCompletionHandler: GADMobileAds.sharedInstance 上的方法,它会初始化 SDK 并在初始化完成后(或 30 秒超时后)回调完成处理程序。这只需要完成一次,最好是在应用程序启动时。您应该尽早致电 startWithCompletionHandler:。

广告可能会在调用 startWithCompletionHandler 时由移动广告 SDK 或中介合作伙伴 SDK 预加载:。如果您需要获得欧洲经济区 (EEA) 用户的同意,请设置任何特定于请求的标志(例如 tagForChildDirectedTreatmenttag_for_under_age_of_consent),或在加载广告之前采取其他措施,请确保在初始化移动广告 SDK 之前执行此操作。

下面是如何调用 startWithCompletionHandler: 方法的示例 AppDelegate:

对于Swift

import GoogleMobileAds

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  func application(_ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    GADMobileAds.sharedInstance().start(completionHandler: nil)

    return true
  }

}

对于Objective-C

@import GoogleMobileAds;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  [[GADMobileAds sharedInstance] startWithCompletionHandler:nil];
  return YES;
}

@end

如果您正在使用中介,您可能希望等到完成处理程序被调用后再加载广告,因为这将确保所有中介适配器都已初始化。