iOS和Mopub:在慢速网络上加载广告时,应用程序冻结 [英] iOS & Mopub: app freezes when loading ad on a slow network

查看:104
本文介绍了iOS和Mopub:在慢速网络上加载广告时,应用程序冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下所示,如《入门指南》中的链接所示: http://help.mopub.com/customer/portal/articles/82831 -开始指南

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

问题是当我启动应用程序时,它将启动viewDidLoad函数,该函数将加载广告.当网络速度非常慢或不存在时,广告的加载会将应用程序的执行冻结20秒钟左右.这是不可接受的行为. 有解决方案吗?

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 ?

推荐答案

您可以尝试将loadAd方法链接到计时器,或者更好地使用基于块的可达性. 您可以从此处获得可达性.

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:在慢速网络上加载广告时,应用程序冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆