Skip to content

广告配置,启动广告,支持静态广告、动态广告、视频

Notifications You must be signed in to change notification settings

struggleypl/YPLaunchAd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

YPLaunchAd

广告配置 启动广告,支持静态广告、动态广告、视频

功能

  • 支持静态/动态图片广告.
  • 支持mp4视频广告.
  • 支持全屏/半屏广告.
  • 支持网络及本地资源.
  • 支持广告点击事件.
  • 支持自定义跳过按钮,添加子视图.
  • 支持设置数据等待时间.
  • 自带图片/视频下载,缓存功能.
  • 支持预缓存图片及视频.
  • 支持设置完成动画.
  • 支持清除指定资源缓存.

使用

添加图片开屏广告

#pragma mark - 图片开屏广告-网络数据-示例 //图片开屏广告 - 网络数据 -(void)example01{

//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[YPLaunchAd setLaunchSourceType:SourceTypeLaunchImage];

//1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
//2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
//3.数据获取成功,配置广告数据后,自动结束等待,显示广告
//注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
[YPLaunchAd setWaitDataDuration:3];

//广告数据请求
[YPLaunchRequest getLaunchAdImageDataSuccess:^(NSDictionary * response) {
    NSLog(@"广告数据 = %@",response);
    //广告数据转模型
    LanuchAdModel *model = [[LanuchAdModel alloc] initWithDict:response[@"data"]];
    //配置广告数据
    YPLaunchImageAdConfiguration *imageAdconfiguration = [YPLaunchImageAdConfiguration new];
    //广告停留时间
    imageAdconfiguration.duration = model.duration;
    //广告frame
    imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.8);
    //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
    imageAdconfiguration.imageNameOrURLString = model.content;
    //设置GIF动图是否只循环播放一次(仅对动图设置有效)
    imageAdconfiguration.GIFImageCycleOnce = NO;
    //缓存机制(仅对网络图片有效)
    //为告展示效果更好,可设置为YPLaunchAdImageCacheInBackground,先缓存,下次显示
    imageAdconfiguration.imageOption = YPLaunchAdImageDefault;
    //图片填充模式
    imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
    //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
    imageAdconfiguration.openModel = model.openUrl;
    //广告显示完成动画
    imageAdconfiguration.showFinishAnimate =ShowFinishAnimateLite;
    //广告显示完成动画时间
    imageAdconfiguration.showFinishAnimateTime = 0.8;
    //跳过按钮类型
    imageAdconfiguration.skipButtonType = SkipTypeTimeText;
    //后台返回时,是否显示广告
    imageAdconfiguration.showEnterForeground = NO;
    
    //图片已缓存 - 显示一个 "已预载" 视图 (可选)
    if([YPLaunchAd checkImageInCacheWithURL:[NSURL URLWithString:model.content]]){
        //设置要添加的自定义视图(可选)
        imageAdconfiguration.subViews = [self launchAdSubViews_alreadyView];
        
    }
    //显示开屏广告
    [YPLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
    
} failure:^(NSError *error) {
}];

}

#pragma mark - 图片开屏广告-本地数据-示例 //图片开屏广告 - 本地数据 -(void)example02{

//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[YPLaunchAd setLaunchSourceType:SourceTypeLaunchImage];

//配置广告数据
YPLaunchImageAdConfiguration *imageAdconfiguration = [YPLaunchImageAdConfiguration new];
//广告停留时间
imageAdconfiguration.duration = 5;
//广告frame
imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.8);
//广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
imageAdconfiguration.imageNameOrURLString = @"image2.jpg";
//设置GIF动图是否只循环播放一次(仅对动图设置有效)
imageAdconfiguration.GIFImageCycleOnce = NO;
//图片填充模式
imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
imageAdconfiguration.openModel = @"http://www.it7090.com";
//广告显示完成动画
imageAdconfiguration.showFinishAnimate =ShowFinishAnimateFlipFromLeft;
//广告显示完成动画时间
imageAdconfiguration.showFinishAnimateTime = 0.8;
//跳过按钮类型
imageAdconfiguration.skipButtonType = SkipTypeRoundProgressText;
//后台返回时,是否显示广告
imageAdconfiguration.showEnterForeground = NO;
//设置要添加的子视图(可选)
//imageAdconfiguration.subViews = [self launchAdSubViews];
//显示开屏广告
[YPLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];

}

#pragma mark - 视频开屏广告-网络数据-示例 //视频开屏广告 - 网络数据 -(void)example03{

//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[YPLaunchAd setLaunchSourceType:SourceTypeLaunchImage];

//1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
//2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
//3.数据获取成功,配置广告数据后,自动结束等待,显示广告
//注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
[YPLaunchAd setWaitDataDuration:3];

//广告数据请求
[YPLaunchRequest getLaunchAdVideoDataSuccess:^(NSDictionary * response) {
    
    NSLog(@"广告数据 = %@",response);
    
    //广告数据转模型
    LanuchAdModel *model = [[LanuchAdModel alloc] initWithDict:response[@"data"]];
    
    //配置广告数据
    YPLaunchVideoAdConfiguration *videoAdconfiguration = [YPLaunchVideoAdConfiguration new];
    //广告停留时间
    videoAdconfiguration.duration = model.duration;
    //广告frame
    videoAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    //广告视频URLString/或本地视频名(请带上后缀)
    //注意:视频广告只支持先缓存,下次显示(看效果请二次运行)
    videoAdconfiguration.videoNameOrURLString = model.content;
    //是否关闭音频
    videoAdconfiguration.muted = NO;
    //视频缩放模式
    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;
    //视频已缓存 - 显示一个 "已预载" 视图 (可选)
    if([YPLaunchAd checkVideoInCacheWithURL:[NSURL URLWithString:model.content]]){
        //设置要添加的自定义视图(可选)
        videoAdconfiguration.subViews = [self launchAdSubViews_alreadyView];
        
    }
    
    [YPLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];
    
} failure:^(NSError *error) {
    
}];

}

#pragma mark - 视频开屏广告-本地数据-示例 //视频开屏广告 - 本地数据 -(void)example04{

//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[YPLaunchAd setLaunchSourceType:SourceTypeLaunchImage];

//配置广告数据
YPLaunchVideoAdConfiguration *videoAdconfiguration = [YPLaunchVideoAdConfiguration new];
//广告停留时间
videoAdconfiguration.duration = 5;
//广告frame
videoAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
//广告视频URLString/或本地视频名(请带上后缀)
videoAdconfiguration.videoNameOrURLString = @"video0.mp4";
//是否关闭音频
videoAdconfiguration.muted = NO;
//视频填充模式
videoAdconfiguration.videoGravity = AVLayerVideoGravityResizeAspectFill;
//是否只循环播放一次
videoAdconfiguration.videoCycleOnce = NO;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
videoAdconfiguration.openModel =  @"http://www.it7090.com";
//跳过按钮类型
videoAdconfiguration.skipButtonType = SkipTypeRoundProgressTime;
//广告显示完成动画
videoAdconfiguration.showFinishAnimate = ShowFinishAnimateLite;
//广告显示完成动画时间
videoAdconfiguration.showFinishAnimateTime = 0.8;
//后台返回时,是否显示广告
videoAdconfiguration.showEnterForeground = NO;
//设置要添加的子视图(可选)
//videoAdconfiguration.subViews = [self launchAdSubViews];
//显示开屏广告
[YPLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];

} #pragma mark - 自定义跳过按钮-示例 -(void)example05{

//注意:
//1.自定义跳过按钮很简单,configuration有一个customSkipView属性.
//2.自定义一个跳过的view 赋值给configuration.customSkipView属性便可替换默认跳过按钮,如下:

//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[YPLaunchAd setLaunchSourceType:SourceTypeLaunchImage];

//配置广告数据
YPLaunchImageAdConfiguration *imageAdconfiguration = [YPLaunchImageAdConfiguration new];
//广告停留时间
imageAdconfiguration.duration = 5;
//广告frame
imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width/1242*1786);
//广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
imageAdconfiguration.imageNameOrURLString = @"image11.gif";
//缓存机制(仅对网络图片有效)
imageAdconfiguration.imageOption = YPLaunchAdImageDefault;
//图片填充模式
imageAdconfiguration.contentMode = UIViewContentModeScaleToFill;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
imageAdconfiguration.openModel = @"http://www.it7090.com";
//广告显示完成动画
imageAdconfiguration.showFinishAnimate = ShowFinishAnimateFlipFromLeft;
//广告显示完成动画时间
imageAdconfiguration.showFinishAnimateTime = 0.8;
//后台返回时,是否显示广告
imageAdconfiguration.showEnterForeground = NO;

//设置要添加的子视图(可选)
imageAdconfiguration.subViews = [self launchAdSubViews];

//start********************自定义跳过按钮**************************
imageAdconfiguration.customSkipView = [self customSkipView];
//********************自定义跳过按钮*****************************end

//显示开屏广告
[YPLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];

}

#pragma mark - 使用默认配置快速初始化 - 示例 /**

  • 图片 */
-(void)example06{
    
    //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
    [YPLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
    
    //使用默认配置
    YPLaunchImageAdConfiguration *imageAdconfiguration = [YPLaunchImageAdConfiguration defaultConfiguration];
    //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
    imageAdconfiguration.imageNameOrURLString = imageURL3;
    //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
    imageAdconfiguration.openModel = @"http://www.it7090.com";
    [YPLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
}

/**

  • 视频 */
-(void)example07{
    
    //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
    [YPLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
    
    //使用默认配置
    YPLaunchVideoAdConfiguration *videoAdconfiguration = [YPLaunchVideoAdConfiguration defaultConfiguration];
    //广告视频URLString/或本地视频名(请带上后缀)
    videoAdconfiguration.videoNameOrURLString = @"video0.mp4";
    //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
    videoAdconfiguration.openModel = @"http://www.it7090.com";
    [YPLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];
}

#pragma mark - 批量下载并缓存 /**

  • 批量下载并缓存图片 */
-(void)batchDownloadImageAndCache{
    
    [YPLaunchAd downLoadImageAndCacheWithURLArray:@[[NSURL URLWithString:imageURL1],[NSURL URLWithString:imageURL2],[NSURL URLWithString:imageURL3],[NSURL URLWithString:imageURL4],[NSURL URLWithString:imageURL5]] completed:^(NSArray * _Nonnull completedArray) {
        
        /** 打印批量下载缓存结果 */
        
        //url:图片的url字符串,
        //result:0表示该图片下载失败,1表示该图片下载并缓存完成或本地缓存中已有该图片
        NSLog(@"批量下载缓存图片结果 = %@" ,completedArray);
    }];
}

/**

  • 批量下载并缓存视频 */
-(void)batchDownloadVideoAndCache{
    
    [YPLaunchAd downLoadVideoAndCacheWithURLArray:@[[NSURL URLWithString:videoURL1],[NSURL URLWithString:videoURL2],[NSURL URLWithString:videoURL3]] completed:^(NSArray * _Nonnull completedArray) {
        
        /** 打印批量下载缓存结果 */
        
        //url:视频的url字符串,
        //result:0表示该视频下载失败,1表示该视频下载并缓存完成或本地缓存中已有该视频
        NSLog(@"批量下载缓存视频结果 = %@" ,completedArray);
        
    }];
    
}

#pragma mark - subViews -(NSArray<UIView *> *)launchAdSubViews_alreadyView{

CGFloat y = XH_IPHONEX ? 46:22;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-140, y, 60, 30)];
label.text  = @"已预载";
label.font = [UIFont systemFontOfSize:12];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.layer.cornerRadius = 5.0;
label.layer.masksToBounds = YES;
label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
return [NSArray arrayWithObject:label];

}

-(NSArray<UIView *> *)launchAdSubViews{

CGFloat y = XH_IPHONEX ? 54 : 30;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-170, y, 60, 30)];
label.text  = @"subViews";
label.font = [UIFont systemFontOfSize:12];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.layer.cornerRadius = 5.0;
label.layer.masksToBounds = YES;
label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
return [NSArray arrayWithObject:label];

}

#pragma mark - 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{

//移除广告
[YPLaunchAd removeAndAnimated:YES];

}

#pragma mark - YPLaunchAd delegate - 倒计时回调 /**

  • 倒计时回调
  • @param launchAd YPLaunchAd
  • @param duration 倒计时时间 */ -(void)YPLaunchAd:(YPLaunchAd *)launchAd customSkipView:(UIView *)customSkipView duration:(NSInteger)duration{ //设置自定义跳过按钮时间 UIButton *button = (UIButton *)customSkipView;//此处转换为你之前的类型 //设置时间 [button setTitle:[NSString stringWithFormat:@"自定义%lds",duration] forState:UIControlStateNormal]; }

#pragma mark - YPLaunchAd delegate - 其他 /** 广告点击事件回调 */

-(void)YPLaunchAd:(YPLaunchAd *)launchAd clickAndOpenModel:(id)openModel clickPoint:(CGPoint)clickPoint{
    
    NSLog(@"广告点击事件");
    
    /** openModel即配置广告数据设置的点击广告时打开页面参数(configuration.openModel) */
    if(openModel==nil) return;
    
    YPWebViewController *VC = [[YPWebViewController alloc] init];
    NSString *urlString = (NSString *)openModel;
    VC.URLString = urlString;
    [[YPRouteUtils currentViewController].navigationController pushViewController:VC animated:YES];
}

/**

  • 图片本地读取/或下载完成回调
  • @param launchAd YPLaunchAd
  • @param image 读取/下载的image
  • @param imageData 读取/下载的imageData */
-(void)YPLaunchAd:(YPLaunchAd *)launchAd imageDownLoadFinish:(UIImage *)image imageData:(NSData *)imageData{
    
    NSLog(@"图片下载完成/或本地图片读取完成回调");
}

/**

  • 视频本地读取/或下载完成回调
  • @param launchAd YPLaunchAd
  • @param pathURL 视频保存在本地的path */
-(void)YPLaunchAd:(YPLaunchAd *)launchAd videoDownLoadFinish:(NSURL *)pathURL{
    
    NSLog(@"video下载/加载完成 path = %@",pathURL.absoluteString);
}

/**

  • 视频下载进度回调 */
-(void)YPLaunchAd:(YPLaunchAd *)launchAd videoDownLoadProgress:(float)progress total:(unsigned long long)total current:(unsigned long long)current{
    
    NSLog(@"总大小=%lld,已下载大小=%lld,下载进度=%f",total,current,progress);
}

/**

  • 广告显示完成 */ -(void)YPLaunchAdShowFinish:(YPLaunchAd *)launchAd{

    NSLog(@"广告显示完成"); }

About

广告配置,启动广告,支持静态广告、动态广告、视频

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published