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
Related
The problem is that Apple rejects my app because when the button is clicked to purchase the "no ads" upgrade, it doesn't make the banner disappear until you close the app and re-open it. This is because I initialize all my RevMob code in the viewController.swift file. I have a boolean in place that turns to false as soon as the upgrade is purchase inside this viewController.swift file. So next time you open the app and the viewController loads, the boolean is set to false and it doesn't allow the ads to appear.
Anybody know if this is the wrong way to go about this? Or is there an easy way to make them disappear immediately upon press of the no Ads button without having to close the app and re-open it?
//BANNER AD =======================================
let bannerBlock: () -> Void = {
//Custom method defined below
if UserDefaults.standard.object(forKey: "adsBool") as! Bool == true
{
self.showBannerWithCustomFrame()
}
else
{
//don't show ads because user purchased
}
}
let bannerFailBlock: ((Error?) -> Void) = {error in
NSLog("[RevMob Sample App] Session failed to start with error: \(error!.localizedDescription)")
}
RevMobAds.startSession(withAppID: "00000000000000000000",
withSuccessHandler: bannerBlock,
andFailHandler: bannerFailBlock)
This is how my bannerView is set up in my GameViewController
class GameViewController: UIViewController, RevMobAdsDelegate {
var bannerView:RevMobBannerView?
override func viewDidLoad() {
super.viewDidLoad()
From RevMob's banner documentation, there's a method called hideAd. Calling that method on the IAP callback would solve your problem right?
To hide the banner:
banner!.hideAd()
Let me see if i got your problem correctly, you want to hide your banner as soon as the purchase is made.
First, you need to set the bannerView as a property of your viewController.
Then you have to add this code inside the callback from the purchase success:
viewController.bannerView.removeFromSuperview();
I fixed it! Thanks for your help. I had to move the code from the GameViewController to the GameScene for the bannerAd so that I could remove it as soon as the purchase was made. Works perfectly.
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.
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.
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
}
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?