2017-10-29 91 views
1

在Cocos2d中顯示AdMob獎勵廣告和插頁式廣告我一直試圖在cocos2d中顯示獎勵廣告或插頁式廣告,但沒有成功。 對於間質性我使用下面的代碼通過使用Objective C

- (void)createAndLoadInterstitial { 
    _interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ID"]; 
    _interstitial.delegate = self; 
    [_interstitial loadRequest:[GADRequest request]]; 
} 
/// Tells the delegate an ad request succeeded. 
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad { 
    NSLog(@"interstitialDidReceiveAd"); 
    if (_interstitial.isReady) { 
     //[_interstitial presentFromRootViewController:[CCDirector sharedDirector]]; 
     [_interstitial presentFromRootViewController:self]; 
    } else { 
     NSLog(@"Ad wasn't ready"); 
    } 

} 
/// Tells the delegate an ad request failed. 
- (void)interstitial:(GADInterstitial *)addidFailToReceiveAdWithError:(GADRequestError *)error { 
    NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription]); 
    [self displayBannerAd]; 
} 
/// Tells the delegate that an interstitial will be presented. 
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad { 
    NSLog(@"interstitialWillPresentScreen"); 
} 
/// Tells the delegate the interstitial is to be animated off the screen. 
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad { 
    NSLog(@"interstitialWillDismissScreen"); 
} 
/// Tells the delegate the interstitial had been animated off the screen. 
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad { 
    NSLog(@"interstitialDidDismissScreen"); 
} 
/// Tells the delegate that a user click will open another app 
/// (such as the App Store), backgrounding the current app. 
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad { 
    NSLog(@"interstitialWillLeaveApplication"); 
} 

爲了獎勵廣告,我用下面的代碼

- (void)display_reward_ad{ 
    [GADRewardBasedVideoAd sharedInstance].delegate = self; 
    [[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request] withAdUnitID:@"ID"]; 
} 
- (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd didRewardUserWithReward:(GADAdReward *)reward { 
    NSString *rewardMessage = [NSString stringWithFormat:@"Reward received with currency %@ , amount %lf", reward.type, [reward.amount doubleValue]]; 
    NSLog(@"%@,",rewardMessage); 
} 
- (void)rewardBasedVideoAdDidReceiveAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd { 
    NSLog(@"Reward based video ad is received."); 
    if ([[GADRewardBasedVideoAd sharedInstance] isReady]) { 
     //[[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:[CCDirector sharedDirector]]; 

     UIView *myView = [[UIView alloc] init]; 
     [[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:myView]; 
     [[[CCDirector sharedDirector] openGLView] addSubview:[GADRewardBasedVideoAd sharedInstance]]; 
    } 
} 
- (void)rewardBasedVideoAdDidOpen:(GADRewardBasedVideoAd *)rewardBasedVideoAd { 
    NSLog(@"Opened reward based video ad."); 
} 
- (void)rewardBasedVideoAdDidStartPlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd { 
    NSLog(@"Reward based video ad started playing."); 
} 
- (void)rewardBasedVideoAdDidClose:(GADRewardBasedVideoAd *)rewardBasedVideoAd { 
    NSLog(@"Reward based video ad is closed."); 
} 
- (void)rewardBasedVideoAdWillLeaveApplication:(GADRewardBasedVideoAd *)rewardBasedVideoAd { 
    NSLog(@"Reward based video ad will leave application."); 
} 
- (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd didFailToLoadWithError:(NSError *)error { 
    NSLog(@"Reward based video ad failed to load."); 
} 

任何一個可以告訴我,我在做什麼錯了,我已經嘗試了上面的代碼中的應用它的工作原理,但不能在Cocos2d工作我顯示橫幅廣告沒有任何問題。 任何幫助或代碼片斷將是偉大的。 在先進的感謝

+0

你有什麼錯誤?或者它只是沒有出現。 – trungduc

+0

應用程序保持崩潰是這是錯誤>>>>呈現插頁式拋出異常: - [CCDirectorDisplayLink presentViewController:animated:completion:]:無法識別的選擇器發送到實例0x156dd7b0 – FreelanceIPhoneDeveloper

+1

嘗試替換'[[[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:myView] ''[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:[[UIApplication sharedApplication] delegate] window] rootViewController]]' – trungduc

回答

1

希望這有助於:)

UIViewController* rootViewController = [[UIApplication sharedApplication]delegate] window] rootViewController]]; 
[[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:rootViewController];