我正在尝试为我的Objective-C游戏添加奖励视频。
它给出
此行中的无法初始化'UIViewController * _Nonnull'类型的参数 左值类型为'AdIntegrator * const __strong'
错误。
[[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:self];
我正在使用Admob官方教程,但出现错误。 https://developers.google.com/admob/ios/rewarded-video
完整代码:
#import "AdIntegrator.h"
#import <GoogleMobileAds/GoogleMobileAds.h>
#import <UIKit/UIKit.h>
@implementation AdIntegrator
+ (id)shared{
static AdIntegrator* integrator = nil;
@synchronized(self){
if(integrator == nil){
integrator = [[self alloc] init];
}
}
return integrator;
}
#pragma mark Core Methods
- (void)initAds{
NSLog(@"[Ads] initialization");
[GADRewardBasedVideoAd sharedInstance].delegate = self;
[[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request]
withAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];
}
- (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.");
}
- (void)rewardBasedVideoAdDidOpen:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
NSLog(@"Opened reward based video ad.");
}
- (void)rewardBasedVideoAdDidStartPlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
NSLog(@"Reward based video ad started playing.");
}
- (void)rewardBasedVideoAdDidCompletePlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
NSLog(@"Reward based video ad has completed.");
}
- (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.");
}
-(void)showBanner{
NSLog(@"[Ads] show banner");
}
-(void)hideBanner{
NSLog(@"[Ads] hide banner");
}
-(bool)isBannerVisible{
return true;
}
-(bool)isRewardedVideoAvialable{
return true;
}
-(void)showInterstitial{
NSLog(@"[Ads] show interstitial");
}
-(void)showRewardedVideo{
NSLog(@"[Ads] show rewarded video");
if ([[GADRewardBasedVideoAd sharedInstance] isReady]) {
[[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:self];//This line is problematic.
}
}
-(void)buttonActivated:(NSString*) name{
}
-(bool)buttonVisible:(NSString*)name{
return true;
}
#pragma mark Integration
@end
为什么会出现此错误?我该如何解决?
答案 0 :(得分:0)
使用UIViewController并使用其sharedInstance初始化
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[self.rewardedAd presentFromRootViewController:viewController delegate:self];