ios iAd loading before displaying - ios

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?

Related

Google Mobile Ads v8 - How do you determine which interstitial ad was dismissed?

I have recently upgraded to Google Mobile Ads v8. In my App, I have two Interstitial ads in the same ViewController that get displayed at unique interactions. Because of this, I need to be able to determine which Interstitial ad was dismissed in adDidDismissFullScreenContent so I can advance the UI accordingly.
Right now I am only determining if it was an Interstitial ad that was dismissed.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
if type(of: ad) == GADInterstitialAd.self {
// Interstitial was dismissed
}
}
Ideally, I would like to be able to determine which ad was dismissed by adUnitID (if that is even possible).
Something like this:
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
if ad.adUnitID = "Ad1"{
// Do something.
} else if ad.adUnitID = "Ad2"{
// Do something else.
}
}
How can I determine which interstitial ad was dismissed using Google Mobile Ads v8?

i want to load facebook Interstitial Ad every five minutes in ios(swift)

i added below code in viewdidload method
self.interstitialAd = FBInterstitialAd.init(placementID: "")
self.interstitialAd.delegate = self;
interstitialAd.load()
and call time for every five minutes to load ad
Timer.scheduledTimer(timeInterval: 300.0, target: self, selector: #selector(callInterstitialAdEveryFiveMinutes), userInfo: nil, repeats: true)
#objc func callInterstitialAdEveryFiveMinutes() {
interstitialAd.load()
}
but not showing ad every five minutes and give error
7000 your ad is already load
thanks in advance
As per the docs, you should first close the loaded ad and request a new one:
Do not call loadAd on the FBInterstitialAd while the ad is being shown on the screen. If you need to load another FBInterstitialAd for future use, you can do so after the user closed the current one, for example in the interstitialAdDidClose callback.
So in this case, you'd need to wait till the user closes the first ad:
func interstitialAdDidClose(_ interstitialAd: FBInterstitialAd?) {
print("Interstitial had been closed")
// Consider to add code here to resume your app's flow
// .. Schedule the timer, or show another ad .. //
}
Please read:
Adding Interstitial Ad to your iOS app
Or see this question InterstitialAd failed to load with error?:
The error you were receiving is basically we can't match a Facebook profile to deliver relevant ads.
In timer, place this code
func showInter() {
if self.fullSscreenAd.isAdValid {
self.fullSscreenAd.show(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}
when the first ad is closed, load the interstitial ad so that the load is ready for the next ad.
func interstitialAdDidClose(_ interstitialAd: FBInterstitialAd) {
self.loadinter() }
func loadinter() {
fullSscreenAd = FBInterstitialAd(placementID: "your placement id")
fullSscreenAd.delegate = self
fullSscreenAd.load()
}

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

How do you preload banner iAds in Swift?

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.

Show GameCenter login later if user dismisses login screen at start of game?

We present the GameCenter login screen upon the game's launch. At the end of the game, we show a GameCenter button that lets users view their achievements and the game's leaderboards. If they dismissed the original screen and aren't logged in, how can we present the login screen again? Here's the code we're using, but it's not working.
override func viewDidLoad() {
super.viewDidLoad()
// Configure view
let skView = view as! SKView
skView.multipleTouchEnabled = false
//skView.showsNodeCount = true
//skView.showsFPS = true
// Show intro scene
let introScene = IntroScene(size: skView.bounds.size, controller: self)
introScene.scaleMode = .AspectFill
skView.presentScene(introScene)
// Authenticate GameCenter player
authenticateGameCenterPlayer()
}
private func authenticateGameCenterPlayer() {
var localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in
if ((viewController) != nil) {
self.presentViewController(viewController, animated: true, completion: nil)
} else {
println((GKLocalPlayer.localPlayer().authenticated))
}
}
}
func showLeaderboard() {
// User logged into GameCenter?
if (!GKLocalPlayer.localPlayer().authenticated) {
println("Local player not authenticated")
authenticateGameCenterPlayer()
return
}
// If here, user is authenticated so present leaderboards
var gcViewController = GKGameCenterViewController()
gcViewController.gameCenterDelegate = self
gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
gcViewController.leaderboardIdentifier = "highScoresLeaderboard"
self.showViewController(gcViewController, sender: self)
self.navigationController?.pushViewController(gcViewController, animated: true)
}
I understand Apple documentation mentions this in the the Game Center Programming Guide under Common Tasks When Working with Players > Authenticating a Local Player on the Device.
Important: Game Kit handles opting out of Game Center across all games
that support Game Center. If a player has already declined to create
an account, when your game authenticates the player, it is told there
is no authenticated player. The player never sees an authentication
dialog. Because Game Kit handles this process across all games, your
game should not include its own mechanism to disable Game Center
authentication or ask a player’s permission to authenticate. Instead,
your game should simply authenticate the player every time it launches
and respond appropriately when authentication completes
.
Game Center remembers the users log-in preferences and if the user dismisses the log-in dialog too many times, it will stop being displayed, even when you call localPlayer.authenticateHandler
The recommended way of handling this is to display a message telling the user to log in through the game center app
Almost a year late, but I encountered a similar issue and implemented a type of work around.
Check to see if the user is authenticated, and if not direct them to use the game center deeplink. This will prompt the login screen.
UIApplication.sharedApplication().openURL(NSURL(string: "gamecenter:")!)
If a user chooses not to login, they will be redirected back to your app. If a user logs in, they will have the ability to navigate back to your app via the back "Back to App" button at the top.

Resources