Tonespy
Tonespy

Reputation: 3387

iOS Native AdMob

Trying to show Google Ad in UITablewViewCell using the Native Google Ad documentation. But, I kept getting

Failed Ad Request:  Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show.}

This is my configuration:

I need to display customised Ads at the 3rd row of the table view.

GoogleAdBannerView.swift import UIKit import GoogleMobileAds

class GoogleAdBannerView: GADUnifiedNativeAdView {

    let modMediaView: GADMediaView = {
        let mediaView = GADMediaView()
        mediaView.translatesAutoresizingMaskIntoConstraints = false
        return mediaView
    }()

    let bannerImageView: UIImageView = {
        let imageview = UIImageView()
        imageview.translatesAutoresizingMaskIntoConstraints = false
        return imageview
    }()

    let adTitle: UILabel = {
        let label = UILabel()
        label.font = UIFont.mySFMedium(ofSize: 16)
        label.textColor = UIColor.black.withAlphaComponent(0.87)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    let refreshBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("Refresh", for: .normal)
        btn.setTitleColor(.blue, for: .normal)
        btn.layer.cornerRadius = 2
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor(red:0.85, green:0.85, blue:0.85, alpha:1.0).cgColor
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        setupViews()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews() {

        mediaView = modMediaView
        iconView = bannerImageView
        headlineView = adTitle

        addSubview(mediaView!)
        addSubview(iconView!)
        addSubview(headlineView!)
        addSubview(refreshBtn)

        mediaView?.widthAnchor.constraint(equalTo: widthAnchor, constant: -32).isActive = true
        mediaView?.heightAnchor.constraint(equalToConstant: 160).isActive = true
        mediaView?.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        mediaView?.topAnchor.constraint(equalTo: topAnchor, constant: 16).isActive = true

        iconView?.widthAnchor.constraint(equalToConstant: 40).isActive = true
        iconView?.heightAnchor.constraint(equalToConstant: 40).isActive = true
        iconView?.leftAnchor.constraint(equalTo: leftAnchor, constant: 16).isActive = true
        iconView?.topAnchor.constraint(equalTo: mediaView!.bottomAnchor, constant: 16).isActive = true

        headlineView?.leftAnchor.constraint(equalTo: iconView!.rightAnchor, constant: 16).isActive = true
        headlineView?.topAnchor.constraint(equalTo: iconView!.topAnchor).isActive = true
        headlineView?.rightAnchor.constraint(equalTo: rightAnchor, constant: -16).isActive = true

        refreshBtn.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -16).isActive = true
        refreshBtn.heightAnchor.constraint(equalToConstant: 45).isActive = true
        refreshBtn.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 2/3, constant: -32).isActive = true
        refreshBtn.leftAnchor.constraint(equalTo: leftAnchor, constant: 16).isActive = true
    }
}

The table view cell:

FieldListLiteAdCell.swift

import UIKit
import GoogleMobileAds

class FieldListLiteAdCell: UITableViewCell, GADUnifiedNativeAdLoaderDelegate {

    let mainView: GoogleAdBannerView = {
        let view = GoogleAdBannerView()
        view.backgroundColor = .white
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    var controller: FieldListView? {
        didSet {
            if let _ = controller {
                fetchAds()
            }
        }
    }
    var adLoader: GADAdLoader!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.white
        self.selectionStyle = .none

        addSubview(mainView)

        mainView.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        mainView.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
        mainView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        mainView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        mainView.refreshBtn.addTarget(self, action: #selector(refreshFieldAds(_:)), for: .touchUpInside)

        fetchAds()
    }

    @IBAction func refreshFieldAds(_ sender: UIButton) {
        fetchAds()
    }

    private func fetchAds() {

        adLoader = GADAdLoader(adUnitID: "ca-app-pub-3940256099942544/3986624511", rootViewController: controller, adTypes: [GADAdLoaderAdType.unifiedNative], options: nil)
        adLoader.delegate = self

        let adRequest = GADRequest()
        adRequest.testDevices = [kGADSimulatorID]
        adLoader.load(adRequest)
//        adLoader.load(GADRequest())
    }

    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
        mainView.nativeAd = nativeAd
        nativeAd.delegate = self

        print("Ad has been received.")

        mainView.mediaView?.mediaContent = nativeAd.mediaContent

        (mainView.iconView as? UIImageView)?.image = nativeAd.icon?.image

        (mainView.headlineView as? UILabel)?.text = nativeAd.headline
    }

    func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
    }

    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) {
        print("Failed Ad Request: ", error)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - GADUnifiedNativeAdDelegate implementation
extension FieldListLiteAdCell: GADUnifiedNativeAdDelegate {

    func nativeAdDidRecordClick(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }

    func nativeAdDidRecordImpression(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }

    func nativeAdWillPresentScreen(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }

    func nativeAdWillDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }

    func nativeAdDidDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }

    func nativeAdWillLeaveApplication(_ nativeAd: GADUnifiedNativeAd) {
        print("\(#function) called")
    }
}

I don't know what I am doing wrong. Tried multiple App Unit ID, created multiple accounts, waited for more than 2hrs as suggested by some StackOverflow answers. Ensured device ID is added to the request.

Edit 1

Someone marked the question as possible duplicate, with an un-verified suggestion. I had to make a sample app that implement this same logic and it's working fine.

I've reviewed the Info.plist file to ensure Allow Arbitrary Loads is set to YES. I'm yet to identify the reason behind the error.

Sample GoogleAd Native Implementation

Any help would be appreciated. Thanks.

Upvotes: 1

Views: 2304

Answers (1)

Tonespy
Tonespy

Reputation: 3387

Apparently, I wasn't doing anything wrong.

Commenting out the app-wide UserAgent modification done when app is initialised in app's delegate made the Google Ad Mod work fine.

My UserAgent looks like this:

UserAgent: iOS-CompanyName/versionNumber.

I don't know why my custom user agent is preventing me from being able to get Ads though.

Upvotes: 1

Related Questions