Strange behavior with GoogleMobileAds sdk

北慕城南 提交于 2019-12-12 04:38:28

问题


When I put this method in application(_ application: UIApplication, didFinishLaunchingWithOptions

GADMobileAds.configure(withApplicationID: "MYAPPID")

My ads show up as expected.

However, as soon as I move the GADMobileAds.configure(withApplicationID: "MYAPPID") to a helper method located inside a library framework, and call that helper method instead, the ads do not show up. And there is no log in the console that indicates what's wrong as far as I can tell.

Does anyone know why this is the case?


回答1:


Swift 3.0

Use this delegates in

class ViewController: UIViewController , GADBannerViewDelegate{

     // put this line in viewdidload()
     let bannerview1 : GADBannerView = GADBannerView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
     bannerview1.adUnitID = "ca-app-pub-3940256099942544/2934735716"
     bannerview1.rootViewController = self
     bannerview1.delegate = self
     bannerview1.load(GADRequest())
     self.view.addSubview(bannerview1)

 // Delegates for adv error

    func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
        print(error)
    }

}



回答2:


A part from Google Admob Documentation for iOS:

Initialize the Google Mobile Ads SDK

At app launch, initialize the Google Mobile Ads SDK by calling configureWithApplicationID: in the application:didFinishLaunchingWithOptions: method of AppDelegate.m or AppDelegate.swift. Objective-CSwift AppDelegate.m

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

  // Use Firebase library to configure APIs
  [FIRApp configure];
  [GADMobileAds configureWithApplicationID:@"ca-app-pub-3940256099942544~1458002511"];
  return YES;
}

Initializing the Google Mobile Ads SDK at app launch allows the SDK to fetch app-level settings and perform configuration tasks as early as possible. This can help reduce latency for the initial ad request. Initialization requires an app ID. App IDs are unique identifiers given to mobile apps when they're registered in the AdMob console.

So, IMHO, you must have to call this configure function on application launch.

Hope that helps!



来源:https://stackoverflow.com/questions/43198744/strange-behavior-with-googlemobileads-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!