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

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.
Install Google Admob Ads via pod pod 'Google-Mobile-Ads-SDK'
In AppDelegate > didFinishLaunchingWithOptions, setup/configure Google Admob GADMobileAds.configure(withApplicationID: Constant.googleAdmobAppID)
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)
}
}
Declare a lazy loaded admob banner in the desired class.
class MyController: UITableViewController {
lazy var googleAdMobBanner: GoogleAdMobBanner = {
return GoogleAdMobBanner(sourceTableViewController: self)
}()
}
Lastly, load the Google Admob in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
googleAdMobBanner.loadAdMob()
}

Related

Replace view if no ads is shown - AdMob

I've an application which uses AdMob as advertisement. And every time no ads is shown it's just a "view" with nothing inside it. I am now looking to replace the empty space and add an image. Is this possible?
So, how can I replace my "view" if no ads is shown, and add an image inside it?
I've tried to googling around, but could not find anything about this.
//Request Advertisement
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
//Set up advertisement
advertisementBanner.adUnitID = "ca-app-pub-7225493999040026/4779408290"
advertisementBanner.rootViewController = self
advertisementBanner.delegate = self
advertisementBanner.load(request)
The current result is: If no ads is available there will be just an empty space in the view. I wanna replace this empty space with an image.
Use GADBannerViewDelegate to do this
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
print("adViewDidReceiveAd")
}
func adView(_ bannerView: GADBannerView,
didFailToReceiveAdWithError error: GADRequestError) {
print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
cnstAddViewHeight.constant = 0.0 // you can hide your view or replace with image here
}

AdMob Interstitial: No ad to load (works on one project but not on another)

I've run into a very strange issue with AdMob. On my main project I receive error that there is no ad to load (when trying to fetch GADInterstitial).
However when I created a new project, copied the AdMob related sources, launched the project it loaded the ad without any problem (it looks like it always loads the test ad; I gave it few tries just to be sure).
On both projects I'm using the same Google pods:
pod 'Google-Mobile-Ads-SDK'
pod 'Google/Analytics'
I've been trying various solutions, generating different ad unit id's, tested the new project without analytics, then attached analytics to it. It worked like a charm during every step.
On my main project I got the ad once, strangely it was for ad unit id (banner view). Sadly since then I cannot get neither banner or interstitial ad (I couldn't retrieve the interstitial ad before).
My target platform is iOS 8+.
Sources, the AdMobManager
import GoogleMobileAds
class AdMobManager {
static let sharedInstance = AdMobManager()
private(set) var interstitial: GADInterstitial!
func loadInterstitialAd(delegate: GADInterstitialDelegate) -> GADInterstitial {
interstitial = GADInterstitial(adUnitID: "same-ad-unit-id-for-both-projects")
interstitial.delegate = delegate
let request = GADRequest()
request.testDevices = [ "same-test-id-for-both-projects" ]
interstitial.loadRequest(request)
return interstitial
}
}
And the ViewController
import UIKit
import GoogleMobileAds
class ViewController: UIViewController {
private var interstitial: GADInterstitial!
override func viewDidLoad() {
interstitial = AdMobManager.sharedInstance.loadInterstitialAd(self)
}
}
extension ViewController: GADInterstitialDelegate {
func interstitialDidReceiveAd(ad: GADInterstitial!) {
if interstitial.isReady {
interstitial.presentFromRootViewController(self)
} else {
NSLog("Not ready")
}
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
NSLog("Error: \(error.debugDescription)")
}
}
Cause of my problem was custom User-Agent.
Hope that save somebody hours of debugging since AdMob documentation doesn't cover that.

How to integrate AdMob as a backup for iAds?

import iAd
#IBOutlet weak var Banner: ADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
Banner.hidden = true
Banner.delegate = self
self.canDisplayBannerAds = true
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.Banner.hidden = false
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("Error")
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
Hello,
I am currently developing an iOS app with Xcode 7.2, Swift 2.0, and iOS 9.2. I have implemented iAds, and it works perfectly. However in my region the fill rate is not high, and I would like to use Google's AdMob to advertise on my app, as a backup. I would like for the AdMob banner ad to show up when iAd does not receive an ad. Note that I am new to Swift, and have no knowledge of Objective-C. Thanks.
You can load the AdMob ad (and view) in the didFailToReceiveAdWithError. supposing that this callback will be called when no iAd is available (I think so).
You idea is, you are going to support two add platforms.
In this case, suppose you are trying to load adMob first.
If adMob will fails, simply load iAD.
Similarly if iAD will fail, load adMob.
So in didFail delegate method of adMob, write code to load iAD.
In didFail delegate method of iAD, write code to load adMob.
this way you can get your required result.

How to show a real Admob ad rather than the test ad?

I wrote this piece of code with Swift so that when I push a button an Admob ad shows up, and now my app is showing a test Admob ad. My question is how I can transition from showing a test ad to a real world ad? I'm using the simulator on Xcode by the way since I don't have an iPhone!
import UIKit
import Parse
import GoogleMobileAds
class NewsPageViewController: UIViewController {
var interstitial:GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
self.interstitial = self.createAndLoadAd()
}
func createAndLoadAd() -> GADInterstitial {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
var request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
interstitial.loadRequest(request)
return interstitial
}
#IBAction func buttonTapped(sender: AnyObject) {
if (self.interstitial.isReady) {
self.interstitial.presentFromRootViewController(self)
self.interstitial = self.createAndLoadAd()
}
}
Do this:
1-remove "request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
2- Create an Admob account on google's website, then create an app in your account,then, replace the default adUnitID given in the google's tutorial with the one in the created app.
3- Then, instead of test ad, you might get a full black screen ad. This one is because it takes a few hours before google starts to send real ads. Hope that helps.

iAd Interstitials not showing consistently? And not at all on the simulator

iAd interstitials aren't showing up at all on the iPhone simulator, and they don't show up consistently on my iPhone. I've gone to the Developer settings, changed the fill rate to 100%, and turned on Unlimited Ad Presentation. No difference... an interstitial will generally show the first time it's supposed to, and then won't show again for anywhere from a few minutes to fifteen minutes. No idea what is causing the difference in time.
Also, there doesn't seem to be a way to track if the interstitial is going to show or not / if it actually showed or didn't. I realize there's an interstitial delegate, but it seems that isn't used anymore. The way I am calling my interstitial is using viewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Automatic
Thanks!
So it seems that using requestInterstitialAdPresentation is intended to only be used when your ADInterstitialPresentationPolicy is set to Automatic. When implementing your interstitials using a Manual ADInterstitialPresentationPolicy you must use presentInView to present the ad at your own intervals. When presenting your interstitial in this manner it does not load with its own close button to dismiss itself. So, what I've done is created a UIView to present the interstitial in and used the interstitials delegate methods to dismiss the UIView. The inconsistency with receiving an ad from the iAd network still arises when testing. Sometimes you receive an ad, sometimes it fails to load which allows us to request a new ad, and sometimes nothing happens at all. This just seems to be the nature of iAd's interstitials.
import UIKit
import iAd // Import iAd
class ViewController: UIViewController, ADInterstitialAdDelegate { // Include the delegate
var iAdInterstitial = ADInterstitialAd() // Our ad
var iAdInterstitialView = UIView() // View to present our ad in
var adLoaded = false // Bool to keep track if an ad is loaded or not
override func viewDidLoad() {
super.viewDidLoad()
setupAd()
}
func setupAd() {
// Set presentation to manual so we can choose when to present the interstitial
// Setting this will also fetch an interstitial ad for us
self.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Manual
iAdInterstitial.delegate = self // Set the delegate
// Make our view the same size as the view we will be presenting in
iAdInterstitialView.frame = self.view.bounds
}
func requestNewAd() {
// This will fetch an ad for us
ViewController.prepareInterstitialAds()
println("Requesting new ad")
}
#IBAction func presentAdButton(sender: AnyObject) {
if (adLoaded) {
// We have an ad that is loaded so lets present it
self.view.addSubview(iAdInterstitialView)
iAdInterstitial.presentInView(iAdInterstitialView)
}
else {
// No ad has been loaded
println("Ad not loaded")
}
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
// Kinda works as expected
// Sometimes is called prematurely
// Sometimes takes minutes after ad is dismissed to be called
println("interstitialAdDidUnload")
// Get new ad
adLoaded = false
iAdInterstitialView.removeFromSuperview()
requestNewAd()
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
// Failed to load ad so lets try again
println("didFailWithError: \(error)")
requestNewAd()
}
func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
// There is an ad and it has begun to download
println("interstitialAdWillLoad")
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
// We got an ad
println("interstitialAdDidLoad")
adLoaded = true
}
func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
println("interstitialAdActionShouldBegin")
return true;
}
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
// Done with this ad. Lets get a new one
println("interstitialAdActionDidFinish")
iAdInterstitialView.removeFromSuperview()
adLoaded = false
requestNewAd()
}

Resources