如果廣告無法載入,系統會呼叫 委派方法或完成處理常式 ,提供 NSError
物件。
針對 a GADBannerView
,會呼叫以下內容:
Swift
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error)
Objective-C
- (void)bannerView:(nonnull GADBannerView *)bannerView
didFailToReceiveAdWithError:(nonnull NSError *)error;
以下程式碼片段說明廣告無法載入時可用的資訊:
Swift
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
// Gets the domain from which the error came.
let errorDomain = error.domain
// Gets the error code. See
// https://developers.google.com/admob/ios/api/reference/Enums/GADErrorCode
// for a list of possible codes.
let errorCode = error.code
// Gets an error message.
// For example "Account not approved yet". See
// https://support.google.com/admob/answer/9905175 for explanations of
// common errors.
let errorMessage = error.localizedDescription
// Gets additional response information about the request. See
// https://developers.google.com/admob/ios/response-info for more information.
let responseInfo = (error as NSError).userInfo[GADErrorUserInfoKeyResponseInfo] as? GADResponseInfo
// Gets the underlyingError, if available.
let underlyingError = (error as NSError).userInfo[NSUnderlyingErrorKey] as? Error
if let responseInfo = responseInfo {
print("Received error with domain: \(errorDomain), code: \(errorCode),"
+ "message: \(errorMessage), responseInfo: \(responseInfo),"
+ "underlyingError: \(underlyingError?.localizedDescription ?? "nil")")
}
}
Objective-C
- (void)bannerView:(GADBannerView *)bannerView
didFailToReceiveAdWithError:(NSError *)error {
// Gets the domain from which the error came.
NSString *errorDomain = error.domain;
// Gets the error code. See
// https://developers.google.com/admob/ios/api/reference/Enums/GADErrorCode
// for a list of possible codes.
int errorCode = error.code;
// Gets an error message.
// For example "Account not approved yet". See
// https://support.google.com/admob/answer/9905175 for explanations of
// common errors.
NSString *errorMessage = error.localizedDescription;
// Gets additional response information about the request. See
// https://developers.google.com/admob/ios/response-info for more
// information.
GADResponseInfo *responseInfo = error.userInfo[GADErrorUserInfoKeyResponseInfo];
// Gets the underlyingError, if available.
NSError *underlyingError = error.userInfo[NSUnderlyingErrorKey];
NSLog(@"Received error with domain: %@, code: %ld, message: %@, "
@"responseInfo: %@, underlyingError: %@",
errorDomain, errorCode, errorMessage, responseInfo,
underlyingError.localizedDescription);
}
您可以利用這項資訊,更準確地判斷廣告載入失敗的原因。
尤其是,如果 GADErrorDomain
這個訊息,localizedDescription
可以在這篇說明中心文章中查看更詳盡的說明,以及可以採取哪些行動來解決問題。