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

家住魔仙堡 提交于 2019-12-10 11:09:33

问题


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

    };
}


来源:https://stackoverflow.com/questions/18013603/ios-mopub-app-freezes-when-loading-ad-on-a-slow-network

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!