2

我的代码如下所示,如入门指南中所述,请参阅链接: 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 秒。这是不可接受的行为。有解决方案吗?

4

1 回答 1

1

您可以尝试将 loadAd 方法链接到计时器或更好地使用基于块的可达性。您可以从这里获得可达性。

// 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];

    };
}
于 2014-02-04T14:13:52.637 回答