How to show iAd on a certain SKScene and hide it on the other one

妖精的绣舞 提交于 2019-12-06 13:49:36

The most clean solution is to declare and implement a protocol to let the UIViewController know that it should hide the ad.

@protocol MySceneDelegate <NSObject>
- (void)hideAd;
- (void)showAd;
@end

@interface MyScene : SKScene
@property (weak) id <MySceneDelegate> delegate;
@end

View controller that shows the scene should implement a hideAd method and set itself as a delegate of the scene. Example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view, etc.
    ...

    // Set the delegate
    [scene setDelegate:self];

    // Present the scene.
    [skView presentScene:scene];
}

Then in the scene you don't want to show an ad, you can call the hideAd method of the view controller which was set as a delegate:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    /* Called when a touch begins */

    if ([_delegate respondsToSelector:@selector(hideAd)])
    {
        [_delegate performSelector:@selector(hideAd)];
    }
}

You can tell the view controller to show the ad in the same way. Hope it helps.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!