How do you preload banner iAds in Swift? - ios

I just integrated iAds into my app and they work perfectly. The only problem is that there is a delay on the page before the ad shows up. I'd like to preload the ads as soon as the app is launched. I have one medium rectangle ad and one regular banner ad (on different views in the app).
Medium rectangle is loaded on viewDidLoad() of ReadingVC.swift like this:
var rectangleAdView = ADBannerView(adType: ADAdType.MediumRectangle)
// Show banner ad
rectangleAdView?.delegate = self
With the following functions:
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("bannerViewDidLoadAd - Ad shown on app")
self.view.addSubview(banner)
self.view.layoutIfNeeded()
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError
error: NSError!) {
banner.removeFromSuperview()
self.view.layoutIfNeeded()
}
The regular banner is loaded on viewDidLoad() of LibraryVC.swift in the same fashion, just with ADAdType.Banner instead of ADAdType.MediumRectangle
How can this be done?

There will always be a bit of a delay because the application has to grab the ads from the iAd CDN. Now, if you have a view that shows before either ReadingVC and LibraryVC, you can always try to load the ads there beforehand. To do this, you'd probably need to create a separate singleton class to hold all of your iAd code, and then access that class from all of the places you need to display or try to preload an iAd.

Related

Live Admob Ads have suddenly stopped displaying in my app

In June, Admob adverts worked perfectly and AdMob sent a letter with a verification PIN for me to verify my identity & payment details.
Around the beginning of July, nearly all live ads stopped displaying in my app. I am still making the same number of requests, but impressions are so low I have dropped to £0.00/£0.01 a day. All test ads work correctly.
This issue began around the time I renamed my app (only on the app store display), however; all links to my app in my AdMob account are correct so the name change appears to have made no difference on their front-end UI.
When I debug my app, I get a list of warnings in the output section:
[I-ACS025031] AdMob App ID changed. Original, new: (nil), AppId
My 'GADApplicationIdentifier' value in my info.plist is the same as the 'new' app id.
[I-ACS013003] User property name must start with a letter: _ap
I am not setting any user properties, no idea what this means.
What have I tried?
Setting up new ad units.
Reverting back to an older version of the app.
Contacted AdMob 'support' via a form. They told me my ad serving is being limited. They did not say for how long and it has been around 2/3 weeks (by 'limited', I don't think they meant completely stopped).
Checked for policy violations in my account; nothing is there.
Code I use to display ads:
I have created an 'AdMobDisplayer' class that allows me to set up and display ads; this is called by each view controller. For example, my banner ads code:
View Controller:
let adMobDisplayer = AdMobDisplayer()
#IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView = self.adMobDisplayer.setupAdBannerView(self.bannerView, viewController: self, adUnitId: Constants.timerTabBannerAdId)
self.adMobDisplayer.displayBannerAd(self.bannerView)
}
AdMobDisplayer:
func setupAdBannerView(_ bannerView: GADBannerView, viewController: UIViewController, adUnitId: String, bannerViewDelgate: GADBannerViewDelegate? = nil) -> GADBannerView {
if(checkIfAdsAreDisabled()) {
return bannerView
}
/// Creates a new GADBannerView to be displayed in a view controller
bannerView.adUnitID = adUnitId
/// bannerView.adUnitID = Constants.testBannerAdId
bannerView.rootViewController = viewController
if let delegate = bannerViewDelgate {
bannerView.delegate = delegate
}
return bannerView
}
func displayBannerAd(_ bannerView: GADBannerView) {
if(checkIfAdsAreDisabled()) {
return
}
///Creates a request and loads an advert from AdMob
let request = GADRequest()
request.testDevices = [ "My Device Id" ]
bannerView.load(request)
}
This should display a banner ad in the view. It worked when I first added adverts in, it works for test adverts, but intermittently/rarely for live adverts now.
Find the full application on my GitHub: https://github.com/AlexMarchant98/KeGal-Trainer
Thanks in advance for any help!
I recently worked on GADBannerView in my last app and had almost similar issue, in your case you may need to generate Admob ad id, from their website.
So I fixed this ages ago, but, my fix was to add the required 'NSAppTransportSecurity' keys into my info.plist.
https://developers.google.com/admob/ios/app-transport-security

How to free scene until Interstitial is dismissed?

So I have a an app that uses interstitial ads. Specifically it is a a SpriteKit game written with Swift.
I have code setup that when the user presses the replay button from the game over scene an ad appears and then it changes back to the game scene to replay the game. Now where I am running into problems the scene changes while the interstitial ad is being displayed, sometimes this doesn't happen fast enough and user can tap the restart button again, causing the game to crash.
Is there a way to freeze the screen and ignore any taps while the ad is being called? And also to only have the scene change after the ad is dismissed?
The code when the restart button is pressed;
if restartButton.contains(pointOfTouch) {
score = 0
ballMovementSpeed = 2
displayAd()
delay(2.0) {
self.restartScene()
}
I am a bit confused as to where the interstitialDelegate is placed. I am trying to implement func interstitialDidDismissScreen(ad: GADInterstitial!) {} to trigger a change back to my game scene and nothing happens when I dismiss the ad.
I have tried placing it in override func didMove(to view: SKScene){} as well as when the restart button is pressed and still won't work. This is how i have the ad being called
fun loadAndShow() {
myAd = GADInterstitial()
let requestI = GADRequest()
myAd.setAdUnitID("adID")
requestI.testDevices = [kGADSimulatorID, "test device"]
myAd.delegate = self
myAd.load(requestI)
}
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
if (self.myAd.isReady) {
myAd.present(fromRootViewController: self)
}
}
you can use delegate methods of admob so when interstitial is going to be shown you can remove the restart button or put a condition so that it would not work when ad is shown. Also to pause the game is also important if it is running using isPaused bool.
https://developers.google.com/admob/ios/ad-events

iAd Banner swift , after i added iAd the game started crash

i developed this game , and it works fine like i want it to do , but after i added the iAd banner , when i start the game it would run normally for like 10 sec. and the it zoom in and make every thing very large and then it would give me this error :
Shapes#2[75422:7643415] [AppDeveloper] ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=1 "Service session terminated." UserInfo=0x7fdb09f5b1e0 {ADInternalErrorCode=1002, NSLocalizedDescription=Service session terminated.}
You're missing your didFailToReceiveAdWithError function. You can copy and paste this code in, just make sure you iAd banner matches what is in your project:
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("Error failed to load. Probably due to network connection.")
self.adBannerView.hidden = true//hide the banner
}
The problem is probably the iAd banner cannot load and you don't have the didFailToReceiveAdWithError function which runs when the banner can't load. If the banner doesn't load, and there's no function to handle this, the compiler will complain.
P.S. You may want to insert this function too. This one is the opposite of didFailToReceiveAdWithError, it runs when the banner can load.
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("Banner did load.")
self.adBannerView.hidden = false//show banner
}
You can find more info about iAd here.

Swift SpriteKit iAd

I'm developing a game in Swift and I have a little problem that I couldn't solve. I'm working only with scenes, I have no UIViews. The main scene is where the game runs and when the players dies a new scene will be loaded. I want in that scene, where the player dies, to display a menu (which I did) and to display iAd banners. I tried also with UIViewControllers but I couldn't manage it. I want to make it only in SpriteKit and I don't know how. Could anybody help me please?
If you simply want to display an iAd banner then you'll need to do several things. First, import the iAd framework and import iAd the the top of your code. Then, use this function to display the banner. (oh, and adBannerView should be declared as a global variables within your skscene).
func loadAds()->ADBannerView{
adBannerView = ADBannerView(frame: CGRect.zeroRect)
adBannerView.center = CGPoint(x: adBannerView.center.x, y: view!.frame.size.height - adBannerView.frame.size.height / 2)
adBannerView.delegate = self
self.view?.addSubview(adBannerView)
return adBannerView
}
You may also want to include these functions. This one right here runs when the banner cannot load (this will most likely occur due to a network problem).
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("Ad cannot load")
self.adBannerView.hidden = true
}
This runs when the banner successfully loads.
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("ad did load.")
self.adBannerView.hidden = false
}

ios iAd loading before displaying

I'm working on a iOS game in swift.
I'm trying to add an interstitial ad.
What I would like to do is when the game is over, displaying an Ad.
After closing it, the user can launch another game.
When the game is over I call this method:
func showFullScreenAd() {
if requestingAd == false {
interstitial = ADInterstitialAd()
interstitial!.delegate = self
requestingAd = true
}
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
if interstitial?.loaded==true{
....}
}
The ad will be displayed when interstitialAdDidLoad is called.
So, between showFullScreenAd() and interstitialAdDidLoad some times we may have several seconds (the time for the ad to load) so the user can click on 'new game' and starts the game.
So, the game will begin and then the ad will be displayed (I'm handling the pause mode also) but it's weird to start the game and to have the ad showing up.
I'm thinking about the following case:
doing
interstitial = ADInterstitialAd()
when loading the game
and when the game is over just show the ad (if available), and doing it again after that.
What do you think about this ?
Thanks.
C.C.
You have to add the ad onto the view as a subview right? So load it and add it to the view when you are ready to. I'm assuming that you're doing it before interstitialAdDidLoad at the moment?

Resources