AdMob实时广告未显示在真实设备上

时间:2018-08-26 15:37:46

标签: ios swift xcode admob

我使用了AdMob在页面上提供的ID为ID的广告来测试连接,并且这些广告都可以在模拟器和真实设备上使用。

当我用实时广告的测试设备进行尝试时,它们会显示在模拟器上(在iPad上并不总是出现在横幅上),但是在我的物理设备(iPad)上,它们不会出现在横幅或插页式广告上?

这是为什么?

谢谢。

横幅代码:

func initAdMobBanner() {

    if UIDevice.current.userInterfaceIdiom == .phone {
        // iPhone
        adMobBannerView.adSize =  GADAdSizeFromCGSize(CGSize(width: 320, height: 50))
        adMobBannerView.frame = CGRect(x: (view.frame.size.width/2)-160, y: view.frame.size.height, width: 320, height: 50)
    } else  {
        // iPad
        adMobBannerView.adSize =  GADAdSizeFromCGSize(CGSize(width: 468, height: 60))
        adMobBannerView.frame = CGRect(x: 0, y: view.frame.size.height, width: 468, height: 60)
    }

    adMobBannerView.adUnitID = ADMOB_BANNER_UNIT_ID
    adMobBannerView.rootViewController = self
    adMobBannerView.delegate = self
    self.navigationController?.view.addSubview(adMobBannerView)

    let request = GADRequest()
    if PACConsentInformation.sharedInstance.isRequestLocationInEEAOrUnknown || PACConsentInformation.sharedInstance.consentStatus.rawValue == 1 {
        if PACConsentInformation.sharedInstance.consentStatus.rawValue == 0 || PACConsentInformation.sharedInstance.consentStatus.rawValue == 1 {
            print("no consent (show non-personalised ads) or unknown")
            let extras = GADExtras()
            extras.additionalParameters = ["npa": "1"]
            request.register(extras)
        }
    }
    adMobBannerView.load(request)
}


// Hide the banner
func hideBanner(_ banner: UIView) {
    banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
    banner.isHidden = true
}

// Show the banner
func showBanner(_ banner: UIView) {
    if #available(iOS 11.0, *) {
        banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height - view.safeAreaInsets.bottom, width: banner.frame.size.width, height: banner.frame.size.height)
    } else {
        banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
    }
    banner.isHidden = false
}

// AdMob banner available
func adViewDidReceiveAd(_ view: GADBannerView) {
    if self.navigationController?.visibleViewController == self {
        let translateTransform = CGAffineTransform(translationX: 0, y: adMobBannerView.bounds.size.height)
        adMobBannerView.transform = translateTransform
        showBanner(adMobBannerView)
        UIView.animate(withDuration: 0.5) {
            self.adMobBannerView.transform = CGAffineTransform.identity
        }
    }
}

// NO AdMob banner available
func adView(_ view: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
    // Reposition the banner ad to create a slide up effect
    let translateTransform = CGAffineTransform(translationX: 0, y: adMobBannerView.bounds.size.height)
    adMobBannerView.transform = translateTransform
    hideBanner(adMobBannerView)
    UIView.animate(withDuration: 0.5) {
        self.adMobBannerView.transform = CGAffineTransform.identity
    }
}

@objc func layoutSubviews() {
    if SpeakTimerPro.hasSpeakTimerPro {
    }
    else {
        initAdMobBanner()
    }

}

2 个答案:

答案 0 :(得分:0)

确保添加测试设备,例如:

let request = GADRequest()
request.testDevices = [ "2077ef9a63d2b398840261c8221a0c9b" ] // Sample device ID

要找到设备ID,请检查控制台输出,该输出应类似于:

<Google> To get test ads on this device, call:
request.testDevices = @[ @"2077ef9a63d2b398840261c8221a0c9b" ];

答案 1 :(得分:0)

请注意,新创建的广告单元ID将需要一段时间才能开始投放实时广告。

如果您仍然遇到问题,请与Google admob小组联系,并提供您面临问题的广告单元ID。