Github地址:https://github.com/CoderZhuXH/XHLaunchAd
1.在didFinishLaunchingWithOptions中或UIApplicationDidFinishLaunching时初始化开屏广告
1.1 添加图片开屏广告-使用本地数据
1.1.1 使用默认配置快速初始化
//1.使用默认配置初始化
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//配置广告数据
XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration defaultConfiguration];
//广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
imageAdconfiguration.imageNameOrURLString = @"image0.jpg";
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
imageAdconfiguration.openModel = @"http://www.it7090.com";
//显示图片开屏广告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
1.1.2自定义配置初始化
//2.自定义配置初始化
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//配置广告数据
XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
//广告停留时间
imageAdconfiguration.duration = 5;
//广告frame
imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-150);
//广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
imageAdconfiguration.imageNameOrURLString = @"image0.jpg";
//设置GIF动图是否只循环播放一次(仅对动图设置有效)
imageAdconfiguration.GIFImageCycleOnce = NO;
//网络图片缓存机制(只对网络图片有效)
imageAdconfiguration.imageOption = XHLaunchAdImageRefreshCached;
//图片填充模式
imageAdconfiguration.contentMode = UIViewContentModeScaleToFill;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
imageAdconfiguration.openModel = @"http://www.it7090.com";
//广告显示完成动画
imageAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
//广告显示完成动画时间
imageAdconfiguration.showFinishAnimateTime = 0.8;
//跳过按钮类型
imageAdconfiguration.skipButtonType = SkipTypeTimeText;
//后台返回时,是否显示广告
imageAdconfiguration.showEnterForeground = NO;
//设置要添加的子视图(可选)
//imageAdconfiguration.subViews = ...
//显示图片开屏广告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
1.2 添加图片开屏广告-使用网络数据
1.2.1 使用默认配置快速初始化
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
//2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
//3.数据获取成功,配置广告数据后,自动结束等待,显示广告
//注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
[XHLaunchAd setWaitDataDuration:3];
/**
* 此处用于模拟广告数据请求,实际项目中请做真实请求
*/
[Network getLaunchAdImageDataSuccess:^(NSDictionary * response) {
NSLog(@"广告数据 = %@",response);
//广告数据转模型
LaunchAdModel *model = [[LaunchAdModel alloc] initWithDict:response[@"data"]];
//配置广告数据
XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration defaultConfiguration];
//广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
imageAdconfiguration.imageNameOrURLString = model.content;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
imageAdconfiguration.openModel = model.openUrl;
//显示开屏广告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
} failure:^(NSError *error) {
}];
1.2.2 自定义配置初始化
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
//2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
//3.数据获取成功,配置广告数据后,自动结束等待,显示广告
//注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
[XHLaunchAd setWaitDataDuration:3];
/**
* 此处用于模拟广告数据请求,实际项目中请做真实请求
*/
[Network getLaunchAdImageDataSuccess:^(NSDictionary * response) {
NSLog(@"广告数据 = %@",response);
//广告数据转模型
LaunchAdModel *model = [[LaunchAdModel alloc] initWithDict:response[@"data"]];
//配置广告数据
XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
//广告停留时间
imageAdconfiguration.duration = model.duration;
//广告frame
imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width/model.width*model.height);
//广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
imageAdconfiguration.imageNameOrURLString = model.content;
//设置GIF动图是否只循环播放一次(仅对动图设置有效)
imageAdconfiguration.GIFImageCycleOnce = NO;
//缓存机制(仅对网络图片有效)
//为告展示效果更好,可设置为XHLaunchAdImageCacheInBackground,先缓存,下次显示
imageAdconfiguration.imageOption = XHLaunchAdImageDefault;
//图片填充模式
imageAdconfiguration.contentMode = UIViewContentModeScaleToFill;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
imageAdconfiguration.openModel = model.openUrl;
//广告显示完成动画
imageAdconfiguration.showFinishAnimate =ShowFinishAnimateLite;
//广告显示完成动画时间
imageAdconfiguration.showFinishAnimateTime = 0.8;
//跳过按钮类型
imageAdconfiguration.skipButtonType = SkipTypeTimeText;
//后台返回时,是否显示广告
imageAdconfiguration.showEnterForeground = NO;
//设置要添加的自定义视图(可选)
//imageAdconfiguration.subViews = ...
//显示开屏广告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
} failure:^(NSError *error) {
}];
1.3添加视频开屏广告-使用本地数据
1.3.1 使用默认配置快速初始化
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//1.使用默认配置初始化
XHLaunchVideoAdConfiguration *videoAdconfiguration = [XHLaunchVideoAdConfiguration defaultConfiguration];
//广告视频URLString/或本地视频名(请带上后缀)
videoAdconfiguration.videoNameOrURLString = @"video0.mp4";
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
videoAdconfiguration.openModel = @"http://www.it7090.com";
//显示视频开屏广告
[XHLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];
1.3.2 自定义配置初始化
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//2.自定义配置
XHLaunchVideoAdConfiguration *videoAdconfiguration = [XHLaunchVideoAdConfiguration new];
//广告停留时间
videoAdconfiguration.duration = 5;
//广告frame
videoAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
//广告视频URLString/或本地视频名(请带上后缀)
videoAdconfiguration.videoNameOrURLString = @"video1.mp4";
//视频填充模式
videoAdconfiguration.videoGravity = AVLayerVideoGravityResizeAspectFill;
//是否只循环播放一次
videoAdconfiguration.videoCycleOnce = NO;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
videoAdconfiguration.openModel = @"http://www.it7090.com";
//广告显示完成动画
videoAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
//广告显示完成动画时间
videoAdconfiguration.showFinishAnimateTime = 0.8;
//跳过按钮类型
videoAdconfiguration.skipButtonType = SkipTypeTimeText;
//后台返回时,是否显示广告
videoAdconfiguration.showEnterForeground = NO;
//设置要添加的子视图(可选)
//videoAdconfiguration.subViews = ...
//显示视频开屏广告
[XHLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];
1.4添加视频开屏广告-使用网络数据
1.4.1 使用默认配置快速初始化
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
//2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
//3.数据获取成功,配置广告数据后,自动结束等待,显示广告
//注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
[XHLaunchAd setWaitDataDuration:3];
/**
* 此处用于模拟广告数据请求,实际项目中请做真实请求
*/
[Network getLaunchAdVideoDataSuccess:^(NSDictionary * response) {
NSLog(@"广告数据 = %@",response);
//广告数据转模型
LaunchAdModel *model = [[LaunchAdModel alloc] initWithDict:response[@"data"]];
//配置广告数据
XHLaunchVideoAdConfiguration *videoAdconfiguration = [XHLaunchVideoAdConfiguration defaultConfiguration];
//注意:视频广告只支持先缓存,下次显示(看效果请二次运行)
videoAdconfiguration.videoNameOrURLString = model.content;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
videoAdconfiguration.openModel = model.openUrl;
[XHLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];
} failure:^(NSError *error) {
}];
1.4.2 自定义配置初始化
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
//2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
//3.数据获取成功,配置广告数据后,自动结束等待,显示广告
//注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
[XHLaunchAd setWaitDataDuration:3];
/**
* 此处用于模拟广告数据请求,实际项目中请做真实请求
*/
[Network getLaunchAdVideoDataSuccess:^(NSDictionary * response) {
NSLog(@"广告数据 = %@",response);
//广告数据转模型
LaunchAdModel *model = [[LaunchAdModel alloc] initWithDict:response[@"data"]];
//配置广告数据
XHLaunchVideoAdConfiguration *videoAdconfiguration = [XHLaunchVideoAdConfiguration new];
//广告停留时间
videoAdconfiguration.duration = model.duration;
//广告frame
videoAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width/model.width*model.height);
//广告视频URLString/或本地视频名(请带上后缀)
//注意:视频广告只支持先缓存,下次显示(看效果请二次运行)
videoAdconfiguration.videoNameOrURLString = model.content;
//视频填充模式
videoAdconfiguration.videoGravity = AVLayerVideoGravityResizeAspectFill;
//是否只循环播放一次
videoAdconfiguration.videoCycleOnce = NO;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
videoAdconfiguration.openModel = model.openUrl;
//广告显示完成动画
videoAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
//广告显示完成动画时间
videoAdconfiguration.showFinishAnimateTime = 0.8;
//后台返回时,是否显示广告
videoAdconfiguration.showEnterForeground = NO;
//跳过按钮类型
videoAdconfiguration.skipButtonType = SkipTypeTimeText;
//设置要添加的自定义视图(可选)
//videoAdconfiguration.subViews = ...
[XHLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];
} failure:^(NSError *error) {
}];
1.5.0 显示完成动画支持以下效果
/** 显示完成动画类型 */
typedef NS_ENUM(NSInteger , ShowFinishAnimate) {
/** 无动画 */
ShowFinishAnimateNone = 1,
/** 普通淡入(default) */
ShowFinishAnimateFadein = 2,
/** 放大淡入 */
ShowFinishAnimateLite = 3,
/** 左右翻转(类似网易云音乐) */
ShowFinishAnimateFlipFromLeft = 4,
/** 下上翻转 */
ShowFinishAnimateFlipFromBottom = 5,
/** 向上翻页 */
ShowFinishAnimateCurlUp = 6,
};
1.6.0 跳过按钮支持以下类型
/** 跳过按钮类型 */
typedef NS_ENUM(NSInteger,SkipType) {
SkipTypeNone = 1,//无
/** 方形 */
SkipTypeTime = 2,//方形:倒计时
SkipTypeText = 3,//方形:跳过
SkipTypeTimeText = 4,//方形:倒计时+跳过 (default)
/** 圆形 */
SkipTypeRoundTime = 5,//圆形:倒计时
SkipTypeRoundText = 6,//圆形:跳过
SkipTypeRoundProgressTime = 7,//圆形:进度圈+倒计时
SkipTypeRoundProgressText = 8,//圆形:进度圈+跳过
};
2.点击事件
/**
广告点击事件代理方法
*/
-(void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenModel:(id)openModel clickPoint:(CGPoint)clickPoint{
NSLog(@"广告点击事件");
/** openModel即配置广告数据设置的点击广告时打开页面参数(configuration.openModel) */
if(openModel==nil) return;
NSString *urlString = (NSString *)openModel;
//此处跳转页面
//WebViewController *VC = [[WebViewController alloc] init];
//VC.URLString = urlString;
////此处不要直接取keyWindow
//UIViewController* rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
//[rootVC.myNavigationController pushViewController:VC animated:YES];
}
3.自定义跳过按钮
//1.XHLaunchImageAdConfiguration 和XHLaunchVideoAdConfiguration 均有一个configuration.customSkipView 属性
//2.自定义一个skipView 赋值给configuration.customSkipView属性 便可替换默认跳过按钮 如下:
configuration.customSkipView = [self customSkipView];
-(UIView *)customSkipView
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor =[UIColor orangeColor];
button.layer.cornerRadius = 5.0;
button.layer.borderWidth = 1.5;
button.layer.borderColor = [UIColor lightGrayColor].CGColor;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
CGFloat y = XH_IPHONEX ? 54 : 30;
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-100,y, 85, 30);
[button addTarget:self action:@selector(skipAction) forControlEvents:UIControlEventTouchUpInside];
return button;
}
-(void)skipAction
{
//移除广告
[XHLaunchAd removeAndAnimated:YES];
}
/**
* 代理方法 - 倒计时回调
*
* @param launchAd XHLaunchAd
* @param duration 倒计时时间
*/
-(void)xhLaunchAd:(XHLaunchAd *)launchAd customSkipView:(UIView *)customSkipView duration:(NSInteger)duration
{
UIButton *button = (UIButton *)customSkipView;//此处转换为你之前的类型
//设置自定义跳过按钮倒计时
[button setTitle:[NSString stringWithFormat:@"自定义%lds",duration] forState:UIControlStateNormal];
}
4.批量下载缓存接口(如果你需要提前批量下载并缓存广告图片或视频请调用下面方法)
/**
* 批量下载并缓存image(异步) - 已缓存的image不会再次下载缓存
*
* @param urlArray image URL Array
*/
+(void)downLoadImageAndCacheWithURLArray:(NSArray * )urlArray;
/**
批量下载并缓存image,并回调结果(异步)- 已缓存的image不会再次下载缓存
@param urlArray image URL Array
@param completedBlock 回调结果为一个字典数组,url:图片的url字符串,result:0表示该图片下载缓存失败,1表示该图片下载并缓存完成或本地缓存中已有该图片
*/
+(void)downLoadImageAndCacheWithURLArray:(nonnull NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock;
/**
* 批量下载并缓存视频(异步) - 已缓存的视频不会再次下载缓存
*
* @param urlArray 视频URL Array
*/
+(void)downLoadVideoAndCacheWithURLArray:(NSArray * )urlArray;
/**
批量下载并缓存视频,并回调结果(异步) - 已缓存的视频不会再次下载缓存
@param urlArray 视频URL Array
@param completedBlock 回调结果为一个字典数组,url:视频的url字符串,result:0表示该视频下载缓存失败,1表示该视频下载并缓存完成或本地缓存中已有该视频
*/
+(void)downLoadVideoAndCacheWithURLArray:(nonnull NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock;
5.检测是否已缓存
/**
* 是否已缓存在该图片
*
* @param url image url
*
* @return BOOL
*/
+(BOOL)checkImageInCacheWithURL:(NSURL *)url;
/**
* 是否已缓存该视频
*
* @param url video url
*
* @return BOOL
*/
+(BOOL)checkVideoInCacheWithURL:(NSURL *)url;
6.缓存/清理相关
/**
* 清除XHLaunch本地所有缓存
*/
+(void)clearDiskCache;
/**
清除指定Url的图片本地缓存(异步)
@param imageUrlArray 需要清除缓存的图片Url数组
*/
+(void)clearDiskCacheWithImageUrlArray:(NSArray *)imageUrlArray;
/**
清除指定Url除外的图片本地缓存(异步)
@param exceptImageUrlArray 此url数组的图片缓存将被保留,不会被清理
*/
+(void)clearDiskCacheExceptImageUrlArray:(NSArray *)exceptImageUrlArray;
/**
清除指定Url的视频本地缓存(异步)
@param videoUrlArray 需要清除缓存的视频url数组
*/
+(void)clearDiskCacheWithVideoUrlArray:(NSArray *)videoUrlArray;
/**
清除指定Url除外的视频本地缓存(异步)
@param exceptVideoUrlArray 此url数组的视频缓存将被保留,不会被清理
*/
+(void)clearDiskCacheExceptVideoUrlArray:(NSArray *)exceptVideoUrlArray;
/**
* 获取XHLaunch本地缓存大小(M)
*/
+(float)diskCacheSize;
/**
* 缓存路径
*/
+(NSString *)xhLaunchAdCachePath;
7.其它代理方法
/**
* 图片本地读取/或下载完成回调
*
* @param launchAd XHLaunchAd
* @param image 读取/下载的image
* @param imageData 读取/下载的imageData
*/
-(void)xhLaunchAd:(XHLaunchAd *)launchAd imageDownLoadFinish:(UIImage *)image imageData:(NSData *)imageData;
{
NSLog(@"图片下载完成/或本地图片读取完成回调");
}
/**
* 视频下载完成回调
*
* @param launchAd XHLaunchAd
* @param pathURL 视频保存在本地的path
*/
-(void)xhLaunchAd:(XHLaunchAd *)launchAd videoDownLoadFinish:(NSURL *)pathURL
{
NSLog(@"video下载/加载完成/保存path = %@",pathURL.absoluteString);
}
/**
* 视频下载进度回调
*/
-(void)xhLaunchAd:(XHLaunchAd *)launchAd videoDownLoadProgress:(float)progress total:(unsigned long long)total current:(unsigned long long)current
{
NSLog(@"总大小=%lld,已下载大小=%lld,下载进度=%f",total,current,progress);
}
/**
* 广告显示完成
*/
-(void)xhLaunchAdShowFinish:(XHLaunchAd *)launchAd
{
NSLog(@"广告显示完成");
}
/**
如果你想用SDWebImage等框架加载网络广告图片,请实现此代理(注意:实现此方法后,图片缓存将不受XHLaunchAd管理)
@param launchAd XHLaunchAd
@param launchAdImageView launchAdImageView
@param url 图片url
*/
-(void)xhLaunchAd:(XHLaunchAd *)launchAd launchAdImageView:(UIImageView *)launchAdImageView URL:(NSURL *)url
{
[launchAdImageView sd_setImageWithURL:url];
}