通过使用Objective C

通过使用Objective C

问题描述:

在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

希望这有助于:)

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