廣告載入失敗時,系統會呼叫失敗的回呼 (提供 LoadAdError
物件)。
在各種廣告格式中,如果廣告無法載入,系統會呼叫下列回呼:
public void OnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
下列程式碼片段會在廣告無法載入時擷取錯誤資訊:
public void OnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
LoadAdError loadAdError = args.LoadAdError;
// Gets the domain from which the error came.
string domain = loadAdError.GetDomain();
// Gets the error code. See
// https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest
// and https://developers.google.com/admob/ios/api/reference/Enums/GADErrorCode
// for a list of possible codes.
int code = loadAdError.GetCode();
// Gets an error message.
// For example "Account not approved yet". See
// https://support.google.com/admob/answer/9905175 for explanations of
// common errors.
string message = loadAdError.GetMessage();
// Gets the cause of the error, if available.
AdError underlyingError = loadAdError.GetCause();
// All of this information is available via the error's toString() method.
Debug.Log("Load error string: " + loadAdError.ToString());
// Get response information, which may include results of mediation requests.
ResponseInfo responseInfo = loadAdError.GetResponseInfo();
Debug.Log("Response info: " + responseInfo.ToString());
}
這項資訊可用來更準確地判斷出廣告載入失敗的原因。具體而言,如果是 iOS 網域 com.google.admob
和 Android 上的 com.google.android.gms.ads
錯誤,您可以在這篇說明中心文章查看 GetMessage()
的詳細說明,以及可採取的解決方式。