我在iOS应用中使用Admob进行插页式广告。我有一个测试广告设置,只有在我第一次浏览应用流程时它才能正常运行。后续尝试不会显示测试广告或调用委托方法。以下是我GADInterstitial
UIViewController
广告设置
ViewController.h
@interface ViewController : UIViewController <GADInterstitialDelegate>
@property(nonatomic, strong) GADInterstitial *interstitial;
ViewController.m
-(void)viewDidLoad{
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-//adID"];
self.interstitial.delegate = self;
GADRequest *request = [GADRequest request];
// Requests test ads on test devices.
request.testDevices = @[@"//test device ID"];
[self.interstitial loadRequest:request];
}
我从IBAction
拨打广告。如果用户已为应用付费或已多次使用IBAction
,则广告不会显示。如果用户未在特定次数内付费或使用IBAction
,则广告会显示。 NSUserDefaults
执行检查,如果用户在某段时间内执行IBAction
,则触发另一种方法,并递增NSUserDefaults
以跟踪用户执行操作的次数。我尝试使用其他广告网络,同样的NSUserDefault
检查工作正常。
在ViewController.m中viewDidLoad
。
-(void)action{
if (([[NSUserDefaults standardUserDefaults] integerForKey:@"AdShow"] < 2) || ([[NSUserDefaults standardUserDefaults] integerForKey:@"DidBuyInAppPurchase"] == 1)) {
[self doOtherAction];
NSInteger integer = [[NSUserDefaults standardUserDefaults]integerForKey:@"AdShow"];
NSInteger newInteger = integer + 1;
[[NSUserDefaults standardUserDefaults] setInteger:newInteger forKey:@"AdShow"];
}
else{
NSInteger newInteger = 0;
[[NSUserDefaults standardUserDefaults] setInteger:newInteger forKey:@"AdShow"];
if ([self.interstitial isReady]) {
[self.interstitial presentFromRootViewController:self];
}
}
}];
}
委托方法也在viewDidLoad
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
}
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
NSLog(@"interstitialWillDismissScreen");
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
[self doOtherAction];
}
- (void)interstitial:(GADInterstitial *)ad
didFailToReceiveAdWithError:(GADRequestError *)error {
[self doOtherAction];
NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}
答案 0 :(得分:0)
GADInterstitial
应该在viewWillAppear
而不是viewDidLoad
。出于某种原因,将其放入viewDidLoad
会导致其无法加载广告。