[iOS][HOW-TO] Pre-load native express ads

152 views
Skip to first unread message

MadJoh Corporation

unread,
Nov 21, 2016, 7:06:48 AM11/21/16
to Google Mobile Ads SDK Developers
Hello,

I want to have a `NativeAdsHelper` singleton that i will use to request and retrieve my NativeAds.

At the moment, here is what I have:


public class NativeAds: NSObject, GADNativeExpressAdViewDelegate {

 
public static func getInstance() -> NativeAds {...}

 
private let adId = "MY_AD_ID"
 
private var ads = [GADNativeExpressAdView]()

 
public func getAd(_ vc: UIViewController) -> GADNativeExpressAdView?
 
{
      requestAd
()
     
if (ads.count == 0) {return nil}
      let ad
= ads[0]
      ad
.rootViewController = vc
      ads
.remove(at: 0)
     
return ad
 
}

 
private func requestAd()
 
{
     
print("Requesting ad...")
     let ad
= GADNativeExpressAdView()
     ad
.adUnitID = adId
     ad
.rootViewController = UIViewController()
     ad
.delegate = self
     ad
.load(GADRequest())
 
}

 
public func nativeExpressAdViewDidReceiveAd(_ nativeExpressAdView: GADNativeExpressAdView)
 
{
      ads
.append(nativeExpressAdView)
     
print("Ad loaded ! (count = \(ads.count))")
 
}

 
public func nativeExpressAdView(_ nativeExpressAdView: GADNativeExpressAdView, didFailToReceiveAdWithError error: GADRequestError)
 
{
   
print("Ad failed to load... \(error.localizedDescription)...)
  }
}


The problem is, if I do not set the rootViewController before calling the `load()`  function, I get an error sayng I should set the rootVC first. But if I do set it with a new instance of `UIViewController`, I never receive an answer... 

Any idea ?

Thank you for your help,

Valentin.

MadJoh Corporation

unread,
Nov 21, 2016, 8:25:53 AM11/21/16
to Google Mobile Ads SDK Developers
EDIT: Even following the AdMob tutorial, I still do not receive any ad... However, I can show rewarded videos Ad using Mediation.

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Nov 21, 2016, 9:58:04 AM11/21/16
to Google Mobile Ads SDK Developers
Hi Valentin,

You need to provide the correct context for the rootVC. You can try to load your application's rootVC from AppDelegate and then make the AdRequest for this pattern.

Thanks,
Arjun Busani
Mobile Ads SDK Team

MadJoh Corporation

unread,
Nov 21, 2016, 11:38:20 AM11/21/16
to Google Mobile Ads SDK Developers
Hello,

Thank you for your answer, however, I still do not receive any ad or error. Looks like nothing is happening. But my delegate looks fine since I receive an error when not setting the rootVC.

Valentin,

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Nov 21, 2016, 11:49:45 AM11/21/16
to Google Mobile Ads SDK Developers
Hi Valentin,

Can you send me a sample app that would reproduce this issue?

Thanks,
Arjun Busani
Mobile Ads SDK Team

MadJoh Corporation

unread,
Nov 21, 2016, 12:00:08 PM11/21/16
to Google Mobile Ads SDK Developers
Hello,

You can find the whole AdMob related code in my first post.

The only thing I can add is that my `GetInstance` function is as follows:

   public static func getInstance() -> NativeAds
   
{

       
if (instance != nil) {return instance}
       
       instance
= NativeAds()
       instance
.initFirstAd()
       
return instance
   
}

   
private func initFirstAd()
   
{
       requestAd
()
   
}

   private var tmpVC : UIViewController!

   private override init()

   {

       super.init()

       let appDelegate = UIApplication.shared.delegate as! AppDelegate

       tmpVC = appDelegate.window!.rootViewController as! ViewController

   }


And I create my instance in my `appDelegate`.

MadJoh Corporation

unread,
Nov 21, 2016, 12:01:55 PM11/21/16
to Google Mobile Ads SDK Developers
Please also note that I do not have any UI at the moment, I'm just trying to get a valid Ad with the delegate.

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Nov 21, 2016, 2:19:37 PM11/21/16
to Google Mobile Ads SDK Developers
Hi Valentin,

You need to add the AdView onto an UIView to get the callbacks. You can get a reference to an UIView, add the AdView as its subview and then make the request. You can also go through our Native Express TableView example, which demonstrates how to preload an ad and show it when needed.

Thanks,
Arjun Busani
Mobile Ads SDK Team

MadJoh Corporation

unread,
Nov 21, 2016, 7:15:45 PM11/21/16
to Google Mobile Ads SDK Developers
Hello,

Thank you for your answer. I'll look into it.

Regards,

MadJoh Corporation

unread,
Nov 22, 2016, 7:16:07 AM11/22/16
to Google Mobile Ads SDK Developers
Hello, 

Thank you for your help, I had to add the ad into a UIView for it to load.

One last question: Now that I can display Native Express and Rewarded Videos ads, I sent my app into alpha testing using testflight. However, my device is the only one receiving ads (I installed via testflight too for testing purposes). I do not have test mode enabled.

Does it take a while for other users to load ads ? Or any other idea ?

Thanks for the support,

Regards,

Valentin
Message has been deleted

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Nov 22, 2016, 10:14:25 AM11/22/16
to Google Mobile Ads SDK Developers
Hi Val,

Currently, we do not have test ads for both Native Express and Reward Video ads. For Native Express, you can use our test Ad Unit ID until your goes for prod and for Reward Video ads, I would suggest you to enable test ads via your Mediation network and not use .addTestDevice for both while requesting ads.

Thanks,
Arjun Busani
Mobile Ads SDK Team

MadJoh Corporation

unread,
Nov 22, 2016, 10:56:54 AM11/22/16
to Google Mobile Ads SDK Developers
Hello,

I am not using test ads because I'm all setup and everything works fine on my phone. 
Moreover I won't enable test Ads while pushing into beta testing because I want to be sure everything works fine before pushing on the App Store.

But my personal phone is the only one receiving Rewarded Ads and I do not understand why.

Regards,

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Nov 22, 2016, 11:43:38 AM11/22/16
to Google Mobile Ads SDK Developers
Hi Val,

If only your device is receiving ads, it sounds like a serving issue with the mediation network. I would suggest you to contact their support and also our AdMob Product Support team to see if there is any issue with your Ad Unit ID. Also, you might want to enable test ads via your mediation network's dashboard to reliably get ads. If you do get them then we can confirm that it is a serving issue and would have to contact their team to fix this.

Thanks,
Arjun Busani
Mobile Ads SDK Team

MadJoh Corporation

unread,
Nov 22, 2016, 6:07:25 PM11/22/16
to Google Mobile Ads SDK Developers
Hello,

Alright, I'll try this. 

Thank you for your time,

Regards,
Reply all
Reply to author
Forward
0 new messages