我无法解决“尝试......其视图不在窗口层次结构中!”当我在Xcode中使用GADInterstitial时。
当我从mainViewController移动到后台并返回我的应用程序时,会出现插页式广告。 但是当我从下一个ViewController移动到后台并回到我的应用程序时,插页式广告没有出现并且发生了错误。 移动到ViewController的方式是模态的。
以同样的方式, 主ViewController - >转到nextViewController - >移动到mainViewController - >转到背景 - >回到app(mainViewController) - >间隙没有出现。
我该如何解决这个问题?
====================================
AppDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSNotification *n = [NSNotification notificationWithName:@"inst" object:self];
}
mainViewController 和 bViewController
(除了“moveToB”的移动目标外,所有代码都相同)
#import "AdMob.h"
- (void)viewDidLoad{
.
.
//if become active, show interstitial
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(showInst) name:@"inst" object:self];
}
-(void)moveToB{
bViewController *nextViewController = [[bViewController alloc] initWithNibName: nil bundle: nil];
[self presentViewController:nextViewController animated:YES completion:nil];
}
//show interstitial
-(void)showInst{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[AdMob sharedInstance].interstitial presentFromRootViewController:self];
});
}
AdMob.m
#import "AdMob.h"
@implementation AdMob
static AdMob *sharedData_ = nil;
+ (AdMob *)sharedInstance{
@synchronized(self){
if (!sharedData_) {
sharedData_ = [[AdMob alloc]init];
}
}
return sharedData_;
}
- (id)init {
self = [super init];
if (self) {
self.interstitial = [self createAndLoadInterstitial];
}
return self;
}
- (GADInterstitial *)createAndLoadInterstitial {
GADInterstitial *interstitial =
[[GADInterstitial alloc] initWithAdUnitID:AdMobInsteID];
interstitial.delegate = self;
[GADRequest request].testDevices = @[ kGADSimulatorID, @"myID" ];
[interstitial loadRequest:[GADRequest request]];
return interstitial;
}
-(void)loadAdMobInste:(UIViewController*)vc rate:(float)rate{
if ([self.interstitial isReady]) {
[self.interstitial presentFromRootViewController:vc];
}
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
NSLog(@"load");
self.interstitial = [self createAndLoadInterstitial];
}
@end