IOS rookie beginner thirteenth chapter: Use XHLaunchAd implement app start advertising network load images error - [UIView subviews] must be used from main thread only

Display ad pages recently started to implement app, and then exposed to a relatively good library ---- XHLaunchAd

There are two access methods, and introduced manually cocoaPods

Personal recommendations will be a path problem with imported cocoaPods, manual, I can not find a solution, with the decisive cocoaPods.

Access procedure used, the success of the static loading local images, no problem.

However, the use of image loading ad server, find the error background thread: - [UIView subviews] must be used from main thread only

7009C5D1-E5B6-4457-A567-B305919F1201

I was NSURLSessionDataTask way to access the requested data.

Format of the data given in the case to write.

There is a problem running thread, meant to operate display pictures in the main thread.

Because the data network requests are asynchronous, then we directly call the asynchronous method --- getLaunchAdImageDataSuccess, in which the initialization --- XHLaunchImageAdConfiguration directly leads to the question, I looked on github, many questions are for this reason .

But there is no return to the main thread initialization XHLaunchImageAdConfiguration the case, therefore, we need to initialize the main thread to operate on

As the following code:

//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
    [XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
    
    //1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
    //2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
    //3.数据获取成功,配置广告数据后,自动结束等待,显示广告
    //注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
    [XHLaunchAd setWaitDataDuration:3];
    
    //广告数据请求
    [Network getLaunchAdImageDataSuccess:^(NSDictionary * response) {
        NSLog(@"广告数据 = %@",response);

        //这里调用返回主线程的方法
        dispatch_async(dispatch_get_main_queue(), ^{
            //广告数据转模型
            LaunchAdModel *model = [[LaunchAdModel alloc] initWithDict:response[@"data"]];
            //配置广告数据
            XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
            //广告停留时间
            imageAdconfiguration.duration = model.duration;
            //广告frame
            imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScre    en].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
            //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
            imageAdconfiguration.imageNameOrURLString = model.content;
            //设置GIF动图是否只循环播放一次(仅对动图设置有效)
            imageAdconfiguration.GIFImageCycleOnce = NO;
            //缓存机制(仅对网络图片有效)
            //为告展示效果更好,可设置为XHLaunchAdImageCacheInBackground,先缓存,下次显示
            imageAdconfiguration.imageOption = XHLaunchAdImageDefault;
            //图片填充模式
            imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
            //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
            imageAdconfiguration.openModel = model.openUrl;
            //广告显示完成动画
            imageAdconfiguration.showFinishAnimate =ShowFinishAnimateLite;
            //广告显示完成动画时间
            imageAdconfiguration.showFinishAnimateTime = 0.8;
            //跳过按钮类型
            imageAdconfiguration.skipButtonType = SkipTypeTimeText;
            //后台返回时,是否显示广告
            imageAdconfiguration.showEnterForeground = NO;
        

            //显示开屏广告
            [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
         });
    } failure:^(NSError *error) {
    }];


 //   下面是设置本地的静态图片


//    //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
//    [XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//
//    //配置广告数据
//    XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
//    //广告停留时间
//    imageAdconfiguration.duration = 3;
//    //广告frame
//    imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
//    //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
//    imageAdconfiguration.imageNameOrURLString = @"image0.jpg";
//    //设置GIF动图是否只循环播放一次(仅对动图设置有效)
//    imageAdconfiguration.GIFImageCycleOnce = NO;
//    //网络图片缓存机制(只对网络图片有效)
//    imageAdconfiguration.imageOption = XHLaunchAdImageRefreshCached;
//    //图片填充模式
//    imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
//    //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
//    imageAdconfiguration.openModel = jumpStrUrl;
//    //广告显示完成动画
//    imageAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
//    //广告显示完成动画时间
//    imageAdconfiguration.showFinishAnimateTime = 0.8;
//    //跳过按钮类型
//    imageAdconfiguration.skipButtonType = SkipTypeTimeText;
//    //后台返回时,是否显示广告
//    imageAdconfiguration.showEnterForeground = NO;
//
//    //设置要添加的子视图(可选)
//    //imageAdconfiguration.subViews = ...
//
//    //显示图片开屏广告
//    [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
}

Thanks XHLaunchAd

Published 65 original articles · won praise 31 · views 50000 +

Guess you like

Origin blog.csdn.net/zhangtao0417/article/details/93881636