2014-08-29 67 views
2

我目前正在開發一個Spritekit項目。Spritekit - 如何隱藏特定場景的iAd橫幅廣告?

我有3個場景:MainMenu的,遊戲,GAMEOVER

我想有網絡成癮顯示,只有當用戶在遊戲場景和GAMEOVER現場。

這是我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場景時出現?

回答

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 
    } 
} 


而在你scense

[[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. 

謝謝,祝你好運。

+0

我試過這個,這對我很有用。謝謝! – aresz 2014-09-01 02:11:34