ios - 无法使用“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
为什么会出现此错误?我该如何解决?
解决方案
使用 UIViewController 并使用其 sharedInstance 进行初始化
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[self.rewardedAd presentFromRootViewController:viewController delegate:self];
推荐阅读
- php - Laravel 自定义验证重定向错误和自定义数据
- vim - rust vim 插件文件类型检测不起作用
- angular - Angular CLI - 使用“ng serve”时不显示任何错误输出
- c# - 在数组中找到左边界的二分查找的递归版本
- c++ - 将 string_view 静态转换为字符串是否合法
- ios - 如何保护 react-native ios 中的 fabric_api 密钥?
- amazon-web-services - symfony 4 prodection 登录在“login_check”上失败
- python - 使用数据框中的现有列分配列值的问题
- configuration - 如何在我的 vagrant 文件中传递本地网络的 dhcp 详细信息
- git - jenkins build git checkout 无法按照 git-parameter 0.9.6 插件的分支选择工作