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 code
property(an Int
) in GADRequestError
so you can use that to create GADErrorCode
as below,
func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
let gadErrorCode = GADErrorCode(rawValue: error.code)
}
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,
GADMobileAds.configure(withApplicationID: "ca-app-pub-3940256099942544~1458002511")
You will get the same error that you have in your question and the delegate method didFailToReceiveAdWithError
will also receive this SDK failure error instead of a banner ad failure error. Actually this error(kCFErrorDomainCFNetwork
code -1009
) means that you are not even connected to an internet connection. You can see here for more detail.
So now if you create GADErrorCode
from this code -1009
, it will always go to the default case
wherever you are using it in a switch
statement. Something as below,
let gadErrorCode = GADErrorCode(rawValue: error.code)!
switch gadErrorCode {
case .internalError:
print( "Internal Error code \(gadErrorCode.rawValue)")
default:
print( "Unknown Error Code \(gadErrorCode.rawValue)")
}
Output
Unknown Error Code -1009
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.configure
and the SDK is successfully initialized then you will always get one of the error code mentioned in the GADErrorCode
enumeration.
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,
bannerView.load(GADRequest())
Now, you will get the error code 2
which is this case case networkError = 2
in GADErrorCode
enum which states,
There was an error loading data from the network.
2) Comment out below line in your code, connect to an internet connection and run your app,
bannerView.rootViewController = self
Now you will get error code 0
which is case invalidRequest = 0
and the reason for this error is mentioned in the documentation as
The ad request is invalid. The localizedFailureReason error description will have more details. Typically this is because the ad did not have the ad unit ID or root view controller set.
So, when the SDK is initialized, you will always get a proper error code
in the delegate
didFailToReceiveAdWithError
.