首页 > 解决方案 > 无法使用“AdIntegrator *const __strong”类型的左值初始化“UIViewController * _Nonnull”类型的参数

问题描述

我正在尝试为我的 Objective-C 游戏添加奖励视频。

它给

无法使用“AdIntegrator *const __strong”类型的左值初始化“UIViewController * _Nonnull”类型的参数

这一行的错误。

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

为什么会出现此错误?我该如何解决?

标签: iosobjective-cxcodeadmobadmob-rewardedvideoad

解决方案


使用 UIViewController 并使用其 sharedInstance 进行初始化

          UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
          [self.rewardedAd presentFromRootViewController:viewController delegate:self];

推荐阅读