I\'ve been trying to figure out how to hide and show iAds in my Spritekit Scenes. Currently I have it setup like this:
ViewController.h
Here is what I did to make it work with SpriteKit Scenes (Xcode 6.1 and iOS 8.1 on iPhone 6):
Step 1- Add #import <"iAd/iAd.h"> in MyScene.h header file
Step 2- Make sure you declare your MyScene class to implement protocol in MyScene.h header file.
Step 3- Add the following code lines in your MyScene.m file inside -(Void)didMoveToView:(SKView *)view function.
ADBannerView* banner=[[ADBannerView alloc]initWithFrame:CGRectZero];
CGRect bannerFrame =CGRectMake(0, 667, self.view.frame.size.width, 0);
banner.frame=bannerFrame;
[self.view addSubview:banner];
banner.delegate=self;
Step 4- Implement the two methods of iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
CGRect bannerFrame =CGRectMake(0, 667-50, self.view.frame.size.width, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
banner.frame=bannerFrame;
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
CGRect bannerFrame =CGRectMake(0, 667, self.view.frame.size.width, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
banner.frame=bannerFrame;
[UIView commitAnimations];
}
The above code will move the Ad frame to scene when there is an Ad and will remove the frame if no Ad by animating the movement. Note that the last number in the frame rect is 0. It doesn't matter what you put there, the banner hight is fixed and doesn't change (50 pt).
Step 5- Respond to Ad actions by this code:
-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
[self startTimer];
}
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
[gameTimer invalidate];
return YES;
}
This code will stop the game timer when a user clicks on the banner and resumes the game timer after the user returns back to the game. You can add your own code for saving and retrieving game data here.
Hope this helps.