I have implemented an AdMob banner view ad, and also the GADBannerViewDelegate protocol as defined here:
https://developers.google.com/admob/ios/banner
So I can use this callback:
/// Tells the delegate an ad request failed.
func adView(_ bannerView: GADBannerView,
didFailToReceiveAdWithError error: GADRequestError) {
print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
I can see from the file that defines GADRequestError that there is also a GADErrorCode enum:
https://github.com/floatinghotpot/google-admob-sdk/blob/master/src/ios/GADRequestError.h
Which is documented here: https://developers.google.com/ad-manager/mobile-ads-sdk/ios/api/reference/Enums/GADErrorCode
-
However I am really struggling trying to get the GADErrorCode enum object from the GADRequestError error object.
-
This is the value of the GADRequestError error object when the ad fails to load because the device is offline:
Error Domain=com.google.admob Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x600000f46880 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "The Internet connection appears to be offline." UserInfo={NSErrorFailingURLStringKey=https://googleads.g.doubleclick.net/mads/static/sdk/native/sdk-core-v40.html?sdk=afma-sdk-i-v7.36.0, NSErrorFailingURLKey=https://googleads.g.doubleclick.net/mads/static/sdk/native/sdk-core-v40.html?sdk=afma-sdk-i-v7.36.0, _kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1, NSLocalizedDescription=The Internet connection appears to be offline.}}, NSErrorFailingURLStringKey=https://googleads.g.doubleclick.net/mads/static/sdk/native/sdk-core-v40.html?sdk=afma-sdk-i-v7.36.0, NSErrorFailingURLKey=https://googleads.g.doubleclick.net/mads/static/sdk/native/sdk-core-v40.html?sdk=afma-sdk-i-v7.36.0, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, NSLocalizedDescription=The Internet connection appears to be offline.}
Can that be converted somehow into a GADErrorCode enum object?
Actually there is a
codeproperty(anInt) inGADRequestErrorso you can use that to createGADErrorCodeas below,Now let's talk about the SDK behavior.
No Internet Connection before Admob SDK initialization
When there is absolutely no internet connection and you initialize the SDK with the following command,
You will get the same error that you have in your question and the delegate method
didFailToReceiveAdWithErrorwill also receive this SDK failure error instead of a banner ad failure error. Actually this error(kCFErrorDomainCFNetworkcode -1009) means that you are not even connected to an internet connection. You can see here for more detail.So now if you create
GADErrorCodefrom this code-1009, it will always go to thedefault casewherever you are using it in aswitchstatement. Something as below,Output
So this was the explanation when SDK is not initialized.
No Internet Connection after Admob SDK initialization OR before banner request
When you had an internet connection while you made the configure call
GADMobileAds.configureand the SDK is successfully initialized then you will always get one of the error code mentioned in theGADErrorCodeenumeration.To verify this we can easily fail banner loading in following two ways
1) You can start your app having an internet connection so that SDK is initialized, then before creating and loading a banner request, just turn off the internet and then call this code,
Now, you will get the
error code 2which is this casecase networkError = 2inGADErrorCodeenum which states,2) Comment out below line in your code, connect to an internet connection and run your app,
Now you will get
error code 0which iscase invalidRequest = 0and the reason for this error is mentioned in the documentation asSo, when the SDK is initialized, you will always get a proper
error codein thedelegatedidFailToReceiveAdWithError.