gpt4 book ai didi

ios - GADInterstitial presentFromRootViewController 导致当前 View Controller 关闭

转载 作者:行者123 更新时间:2023-11-29 12:29:08 25 4
gpt4 key购买 nike

当我的应用程序的用户点击某个按钮转到某个 ViewController 时,我试图展示一个 GADInterstitial 广告。在新的 ViewController 的 viewDidLoad 方法中,我检查 GADInterstitial 广告是否准备就绪,如果准备就绪,我将尝试展示它。问题是,当我关闭广告时,我注意到呈现广告的 ViewController 已不存在,而是将我送回了启动呈现广告的 ViewController 的 ViewController。

这是我的代码:

   - (void)viewDidLoad {
[super viewDidLoad];


BTRInterstitialHelper *helper = [BTRInterstitialHelper sharedHelper];
if([helper isInterstitialReady]) {
[helper.interstitial presentFromRootViewController:self];
}

我的辅助方法如下所示:

    -(BOOL)isInterstitialReady {
if ([self.interstitial isReady]) {
return YES;
} else {
return NO;
}
}

我确实注意到,当我展示广告时,这条消息会出现在日志中:

  Presenting view controllers on detached view controllers is discouraged BTRIndividualLocationViewController: 0x7fd63bcd9c00.

如果我尝试在 viewWillAppear 中显示它,也会出现同样的问题。

我在这里做错了什么?

编辑:我还尝试了以下方法,以尝试传入我的应用程序的实际 rootViewController:

     UINavigationController *nav=(UINavigationController *)((BTRAppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
UIViewController *rootController=(UIViewController *)[nav.viewControllers objectAtIndex:0];

[helper.interstitial presentFromRootViewController:rootController];

虽然结果仍然相同,但消息不会在日志中弹出。

最佳答案

VC2.h

#import "GADInterstitial.h"
#import "GADInterstitialDelegate.h"

设置委托(delegate)

@interface VC2 : UIViewController <GADInterstitialDelegate>

和属性

@property(nonatomic, strong) GADInterstitial *interstitial;

VC2.m在 View 中出现

self.interstitial = [[GADInterstitial alloc] init];
self.interstitial.adUnitID = @"ca-app-pub-yourNumber";
self.interstitial.delegate = self;

GADRequest *request = [GADRequest request];

用于模拟器中的测试广告

request.testDevices = @[ GAD_SIMULATOR_ID ];

还可以在请求中添加位置和关键字(可选)

然后做请求

[self.interstitial loadRequest:request];

听委托(delegate)

-(void)interstitialDidReceiveAd:(GADInterstitial *)ad {

if ([self.interstitial isReady]) {


[self.interstitial presentFromRootViewController:self];
}
}

如果您只想在按下某个按钮时显示插页式广告,请使用 NSUserdefaults。

在按下按钮时在 VC1 中

[[NSUserDefaults standardUserDefaults] setValue:@"yes" forKey:@"showInterstitial"];
[[NSUserDefaults standardUserDefaults] synchronize];

并将VC2中的插页式广告的所有代码包装在viewdidappear中:

if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"showInterstitial"] isEqualToString:@"yes"]) {

}

并添加到 -(void)interstitialDidReceiveAd:(GADInterstitial *)ad {

[[NSUserDefaults standardUserDefaults] setValue:@"no" forKey:@"showInterstitial"];
[[NSUserDefaults standardUserDefaults] synchronize];

附注当然,你也可以为 NSUserdefaults 使用 Bool,我确实使用了字符串,因为我在使用 Bools 时遇到了一些错误,但我猜 Bools 会更正确

关于ios - GADInterstitial presentFromRootViewController 导致当前 View Controller 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28246689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com