I just finished making my first game for ios with swift and spritekit and was thinking about implementing iAds banner or interstitial ad in the main menu. But the problem is that I made the title screen programtically without any storyboards. I searched googled around but I was unable to find any tutorial or a easy to understand sample that teaches me how to implement iAd to the GameScene programatically. I would love some tips or examples from you !
I was planning to integrate iAds within MainScene but cannot find a solution to implement the ad.
import iAd
class MainScene: SKScene , ADBannerViewDelegate {
}
First you need to create two variables:-
var bannerView = ADBannerView()
var isBannerVisible = false
Then in your viewDidLoad or didMove to view put:-
bannerView.frame = CGRectMake(0, self.view!.frame.size.height, self.view!.frame.width, 80)
bannerView.delegate = self
isBannerVisible = false
Next step is to add the following functions:-
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
if(isBannerVisible == false){
if(bannerView.superview == nil){
self.view?.addSubview(bannerView)
}
UIView.beginAnimations("iAdBannerShow", context: nil)
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height)
UIView.commitAnimations()
isBannerVisible = true
}
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
if(isBannerVisible == true){
UIView.beginAnimations("iAdBannerHide", context: nil)
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height)
UIView.commitAnimations()
isBannerVisible = false
}
}
Along with this make sure you add the ADBannerViewDelegate and import iAd which i see you already have, and this should work. Let me know if you have any issues, this is what i use in my swift spritekit games.
Good Luck,
Rachel