iOS & Mopub: app freezes when loading ad on a slow

2019-05-23 05:17发布

问题:

My code look like this as described in the getting started guide see link: 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];
}

The problem is when I start the app, it will start the viewDidLoad function which will load the ad. When the network is very slow or not existing the loading of the ad will freeze the executing of the app for about 20 sec. and this is not acceptable behavior. Is there a solution for this ?

回答1:

You may try linking loadAd method to a timer or better use block based reachability. You can get reachability from 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];

    };
}


标签: ios mopub