简体   繁体   中英

Not able to add Google native ads in my iOS swift app

I have an iOS app in swift language. I have included Google AdMob ads in my app. I have implemented banner ads and interstitial ads but I am not able to generate the Ad ID for Native Ads. I have found an Ad Sense custom search native ads but I don't know for what purpose these ads are used. Can I use AdSense native ads in my mobile app. Please suggest me what to do and how to progress?

Below are the steps that I always follow whenever it comes to adding Google Admob Ads. Do take note that the example below will implement Google Admob in a table view.

  1. Install Google Admob Ads via pod pod 'Google-Mobile-Ads-SDK'
  2. In AppDelegate > didFinishLaunchingWithOptions , setup/configure Google Admob GADMobileAds.configure(withApplicationID: Constant.googleAdmobAppID)

  3. Next, create a class for Google Admob Banner.

import Foundation
import GoogleMobileAds

class GoogleAdMobBanner: NSObject, GADBannerViewDelegate {

    unowned var sourceTableViewController: UITableViewController
    var adBannerView: GADBannerView

    init(sourceTableViewController: UITableViewController) {
        self.sourceTableViewController = sourceTableViewController
        adBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

        super.init()

        adBannerView.adUnitID = Constant.googleAdmobBannerID
        adBannerView.delegate = self
        adBannerView.rootViewController = sourceTableViewController
    }

    // MARK:- Google Admob

    func adViewDidReceiveAd(_ bannerView: GADBannerView) {
        print("Banner loaded successfully")

        // Reposition the banner ad to create a slide up effect
        let translateTransform = CGAffineTransform(translationX: 0, y: -bannerView.bounds.size.height)
        bannerView.transform = translateTransform

        UIView.animate(withDuration: 0.5) {
            bannerView.transform = CGAffineTransform.identity
            self.sourceTableViewController.tableView.tableHeaderView = bannerView
        }
    }

    func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
        print("Fail to receive ads")
        print(error)
    }

    func loadAdMob() {
        let request = GADRequest()
        request.testDevices = [kGADSimulatorID]

        adBannerView.load(request)
    }

}
  1. Declare a lazy loaded admob banner in the desired class.
class MyController: UITableViewController {

    lazy var googleAdMobBanner: GoogleAdMobBanner = {
        return GoogleAdMobBanner(sourceTableViewController: self)
    }()

}
  1. Lastly, load the Google Admob in viewDidLoad
override func viewDidLoad() {
    super.viewDidLoad()

    googleAdMobBanner.loadAdMob()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM