I'm currently developing a very small game with SpriteKit and Swift. All game logic is handled in the gamescene class and the game has three states. Start, play and highscore. When the user starts the app, the function startScreen() gets a call. It displays some information and a button which removes all nodes created in that function when tapped on. Then I spawn the nodes necessary for the game and when you loose the function showHighscore() gets a call and it's basically just like startScreen(), a bunch of labels, sprites and buttons which gets added upon request and then removed from its parent node when the button is tapped. Then the user is taken to the startscreen again and everything I described is starting over.
Now when I've declared the concept of my scenes, it's time for the question.
How on earth do I add an iAd banner and an AdMob banner when the iAd one isn't working? I've searched for hours now and the only thing I've found is in Objective-c. Even the documentation is in Objective-c! I don't know and don't understand Objective-c.
My goal is to display the ads at all time except when the user is playing the game. So it should be displayed when showHighscore() is called and removed when the user taps on the button with the name "play".
The code I got already I found on another Stackoverflow question which gave me a good ground to stand on, but then I read this:
Only create a banner view when you intend to display it to the user. Otherwise, it may cycle through ads and deplete the list of available advertising for your app.
and
If the user navigates from a screen of content with a banner view to a screen that does not have a banner view, and you expect them to be on that screen for a long period of time, remove the banner view from the view hierarchy, set its delegate to nil and release it before transitioning to the new screen of content. More generally, avoid keeping a banner view around when it is invisible to the user.
this on the iAd best practices page (http://ift.tt/1IQxoCE)
Here's the code I got now (no AdMob because I still don't understand the iAd part):
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
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
println("left for ad")
return true
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
println("Back from ad-visit")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("Ad cannot load")
self.adBannerView.hidden = true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("ad did load.")
self.adBannerView.hidden = false
}
This code works. It displays and ad and everything, but I've read on multiple places that this code has to be GameViewController because it subclasses UIViewController. But it works when having it in GameScene as well?
To wrap up the question(s);
- Is it alright to create the ad inside my gamescene subclassing SKScene or will my app get rejected or anything?
- How do I follow Apples practices with removing and adding the banner when not in use? Let us say that I want to show it when I call showHighscore() but remove it when I call startGame()? Should I just use hidden = true or does that not stop the ads from cycling through?
- How do I integrate AdMob (in Swift, their documentation and tutorials are also in Objective-c) when iAd fails to display an ad?
This is a kind of long one, so I'm thankful you made it all the way here. Keep in mind that I'm also using SpriteKit and almost every tutorial featuring this subject with Swift is plain iOS apps, not SpriteKit. I would like something like this when it comes to integrating those two: http://ift.tt/1eIDMYx
Thank you!
Aucun commentaire:
Enregistrer un commentaire