最近要实现app启动时展示广告页,然后接触到一个比较优秀的库----XHLaunchAd
接入的方法有两种,手动导入和cocoaPods
个人建议用cocoaPods,手动导入的会有路径问题,我找不到解决办法,果断用cocoaPods。
接入使用的过程中,成功的加载了静态本地图片,完全没问题。
但是在使用服务器的图片加载广告时,发现报错background thread: -[UIView subviews] must be used from main thread only
我是用NSURLSessionDataTask的方式访问请求数据。
数据的格式按给出的案例来写。
运行出现了线程的问题,意思是要在主线程进行显示图片的操作。
因为网络请求的数据是异步的,然后我们直接调用这个异步的方法---getLaunchAdImageDataSuccess,在其中直接进行初始化---XHLaunchImageAdConfiguration就会导致这样的问题,github上我看了,很多提问都是这个原因。
但是案例上没有返回主线程初始化XHLaunchImageAdConfiguration,因此,我们需要将初始化的操作放在主线程中即可
如下列代码:
//设置你工程的启动页使用的是: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];
}
感谢 XHLaunchAd