我目前正在开发一个Spritekit项目。
我有3个场景:MainMenu,Game,Gameover
我希望只有当用户在游戏场景和Gameover场景时才会有iAd节目。
这是我在ViewController.m中的iAd的当前代码:
- (void) viewWillLayoutSubviews
{
// For iAds
_bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
_bannerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
_bannerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
_bannerView.delegate = self;
_bannerView.hidden = YES;
[self.view addSubview:_bannerView];
}
#pragma mark - iAds delegate methods
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
// Occurs when an ad loads successfully
_bannerView.hidden = NO;
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
// Occurs when an ad fails to load
_bannerView.hidden = YES;
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
// Occurs when the user taps on ad and opens it
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
// Occurs when the ad finishes full screen
}
问题是,由于MainMenu场景是第一个要显示的场景,因此横幅会在成功加载广告时显示。 如何仅在用户处于游戏场景和Gameover场景时才显示?
答案 0 :(得分:5)
这里最好的方法是使用NSNotificationCenter:
在您的(void) viewWillLayoutSubviews
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"hideAd" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];
并在此处理通知
- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:@"hideAd"])
{
// hide your banner;
}else if ([notification.name isEqualToString:@"showAd"])
{
// show your banner
}
}
在你的风景中
[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; //Sends message to viewcontroller to show ad.
[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil]; //Sends message to viewcontroller to hide ad.
谢谢,祝你好运。