我的代码如下所示,如入门指南中所述,请参阅链接: http://help.mopub.com/customer/portal/articles/82831-start-guide
- (void)viewDidLoad {
self.adView = [[MPAdView alloc] initWithAdUnitId:@"xxx" size:MOPUB_BANNER_SIZE];
self.adView.delegate = self;
self.adView.frame = CGRectMake(0, self.view.bounds.size.height - MOPUB_BANNER_SIZE.height, MOPUB_BANNER_SIZE.width, MOPUB_BANNER_SIZE.height);
self.adView.keywords = keywords;
[self.view addSubview:self.adView];
[self.adView loadAd];
[super viewDidLoad];
}
问题是当我启动应用程序时,它将启动将加载广告的viewDidLoad函数。当网络非常慢或不存在时,广告的加载将冻结应用程序的执行约20秒。这是不可接受的行为。 有解决方案吗?
答案 0 :(得分:1)
您可以尝试将loadAd方法链接到计时器或更好地使用基于块的可达性。 您可以从here获得可访问性。
// in view header file
NSTimer * aTimer;
//in implementation
-(void)viewDidLoad
{
...
[self.view addSubview:self.adView];
[self.adview setHidden:YES];
[self loadAdIfReachable];
...
}
-(void) loadAdIfReachable{
// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
NSLog(@"REACHABLE!");
[self.adview setHidden:NO];
[self.adView loadAd];
};
}