header.png
一.前言:
1.之前由于項目需要,寫了一個開屏廣告組件XHLaunchAd,主要自用,后來發現越來越多的開發者,有這個需求.
2.隨著使用人數的增加,部分開發者使用者提出了一些需求和問題,XHLaunchAd并不能很好的解決.
3.于是決定最近抽時間,把這個組件優化重構一下,解決網友之前提出一些問題和需求,并增加一些新功能,發布v3.0.0版本.
4.該項目目前已經收到 700 多個 star ,目前已經更新到v3.0.1版本
5.代碼地址: https://github.com/CoderZhuXH/XHLaunchAd
-> XHLaunchAd (3.0.1)
開屏廣告解決方案,支持圖片/視頻、靜態/動態、全屏/半屏廣告,支持iPhone/iPad,自帶圖片下載、緩存功能,無其他三方依賴
pod 'XHLaunchAd', '~> 3.0.1'
- Homepage: https://github.com/CoderZhuXH/XHLaunchAd
- Source: https://github.com/CoderZhuXH/XHLaunchAd.git
- Versions: 3.0.1, 3.0.0, 2.2.2, 2.2.1, 2.2.0, 2.1.9, 2.1.8, 2.1.7, 2.1.6,
2.1.5, 2.1.4, 2.1.3, 2.1.2, 2.1.1, 2.1.0, 2.0.1, 2.0, 1.2, 1.1.4, 1.1.3,
1.1.2, 1.1, 1.0 [master repo]
二.主要更新:
1.增加mp4視頻開屏廣告
2.增加對本地資源支持
3.增加預緩存接口
4.增加更多屬性及接口,具有更強的自定義性
5.可設置顯示完成動畫類型
6.可自定義跳過按鈕
7.擁有更優雅的接入接口
8.優化緩存機制,bug fix等等
三.接入后效果:
ScreenShot00.gif
ScreenShot06.gif
ScreenShot05.gif
ScreenShot02.gif
ScreenShot01.gif
四.使用方法:
在AppDelegate didFinishLaunchingWithOptions方法中添加下面代碼(提前設置App啟動頁為LaunchImage)
-1.添加圖片開屏廣告??
--1.1使用默認配置快速初始化
//1.使用默認配置初始化
XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration defaultConfiguration];
//廣告圖片URLString/或本地圖片名(.jpg/.gif請帶上后綴)
imageAdconfiguratuin.imageNameOrURLString = @"image0.jpg";
//廣告點擊打開鏈接
imageAdconfiguration.openURLString = @"http://www.returnoc.com";
//顯示圖片開屏廣告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
--1.2自定義配置初始化
//2.自定義配置初始化
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";
//網絡圖片緩存機制(只對網絡圖片有效)
imageAdconfiguration.imageOption = XHLaunchAdImageRefreshCached;
//圖片填充模式
imageAdconfiguration.contentMode = UIViewContentModeScaleToFill;
//廣告點擊打開鏈接
imageAdconfiguration.openURLString = @"http://www.returnoc.com";
//廣告顯示完成動畫
imageAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
//跳過按鈕類型
imageAdconfiguration.skipButtonType = SkipTypeTimeText;
//后臺返回時,是否顯示廣告
imageAdconfiguration.showEnterForeground = NO;
//顯示圖片開屏廣告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
-2.添加視頻開屏廣告??
--2.1 使用默認配置快速初始化
//1.使用默認配置初始化
XHLaunchVideoAdConfiguration *videoAdconfiguration = [XHLaunchVideoAdConfiguration defaultConfiguration];
//廣告視頻URLString/或本地視頻名(請帶上后綴)
videoAdconfiguration.videoNameOrURLString = @"video0.mp4";
//廣告點擊打開鏈接
videoAdconfiguration.openURLString = @"http://www.returnoc.com";
//顯示視頻開屏廣告
[XHLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];
--2.2 自定義配置初始化
//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.scalingMode = MPMovieScalingModeAspectFill;
//廣告點擊打開鏈接
videoAdconfiguration.openURLString = @"http://www.returnoc.com";
//廣告顯示完成動畫
videoAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
//跳過按鈕類型
videoAdconfiguration.skipButtonType = SkipTypeTimeText;
//后臺返回時,是否顯示廣告
videoAdconfiguration.showEnterForeground = NO;
//顯示視頻開屏廣告
[XHLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:self];
注意:
若你的廣告圖片/視頻URL來源于數據請求,請在請求數據前設置等待時間,在數據請求成功回調里,配置廣告,如下:
//1.因為數據請求是異步的,請在數據請求前,調用下面方法配置數據等待時間.
//2.設為3即表示,啟動頁將停留3s等待服務器返回廣告數據,3s內等到廣告數據,將正常顯示廣告,否則將自動進入window的RootVC
//設置數據等待時間
[XHLaunchAd setWaitDataDuration:3];
//廣告數據請求
[Network getLaunchAdImageDataSuccess:^(NSDictionary * response) {
//在此處利用服務器返回的廣告數據,按上面示例添加開屏廣告代碼
XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration ...
//配置相關參數....
//顯示開屏廣告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
} failure:^(NSError *error) {
}];
-3.點擊事件
/**
* 廣告點擊事件 回調
*/
- (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenURLString:(NSString *)openURLString;
{
if(openURLString)
{
//跳轉到廣告詳情頁面
WebViewController *VC = [[WebViewController alloc] init];
VC.URLString = openURLString;
[self.window.rootViewController presentViewController:VC animated:YES completion:nil];
}
}
-4.自定義跳過按鈕
//1.XHLaunchImageAdConfiguration 和XHLaunchVideoAdConfiguration 均有一個configuration.customSkipView 屬性
//2.自定義一個skipView 賦值給configuration.customSkipView屬性 便可替換默認跳過按鈕 如下:
configuration.customSkipView = [self customSkipView];
-(UIView *)customSkipView
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor =[ UIColor clearColor];
button.layer.cornerRadius = 3.0;
button.layer.borderWidth = 1.0;
button.layer.borderColor = [UIColor whiteColor].CGColor;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:13];
button.frame = CGRectMake(15,[UIScreen mainScreen].bounds.size.height-55, 85, 40);
[button addTarget:self action:@selector(skipAction) forControlEvents:UIControlEventTouchUpInside];
return button;
}
-(void)skipAction
{
[XHLaunchAd skipAction];
}
/**
* 代理方法-倒計時回調
*
* @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];
}
-5.預緩存接口(如果你需要提前下載并緩存廣告圖片或視頻 請調用下面方法)
/**
* 批量下載并緩存image(異步)
*
* @param urlArray image URL Array
*/
+(void)downLoadImageAndCacheWithURLArray:(NSArray <NSURL *> * )urlArray;
/**
* 批量下載并緩存視頻(異步)
*
* @param urlArray 視頻URL Array
*/
+(void)downLoadVideoAndCacheWithURLArray:(NSArray <NSURL *> * )urlArray;
-6.其他代理方法
/**
* 圖片下載完成/或本地圖片讀取完成 回調
*
* @param launchAd XHLaunchAd
* @param image image
*/
-(void)xhLaunchAd:(XHLaunchAd *)launchAd imageDownLoadFinish:(UIImage *)image
{
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)xhLaunchShowFinish:(XHLaunchAd *)launchAd
{
NSLog(@"廣告顯示完成");
}
/**
如果你想用SDWebImage等框架加載網絡廣告圖片,請實現此代理
@param launchAd XHLaunchAd
@param launchAdImageView launchAdImageView
@param url 圖片url
*/
-(void)xhLaunchAd:(XHLaunchAd *)launchAd launchAdImageView:(UIImageView *)launchAdImageView URL:(NSURL *)url
{
[launchAdImageView sd_setImageWithURL:url];
}
-7.其他操作
/**
* 清除XHLaunch本地緩存
*/
+(void)clearDiskCache;
/**
* 獲取XHLaunch本地緩存大小(M)
*/
+(float)diskCacheSize;
/**
* 緩存路徑
*/
+(NSString *)xhLaunchAdCachePath;
五.小結:
XHLaunchAd 3.0 擁有更靈活的調用方式和更優雅的接口,后期我還會持續維護并更新他,希望能給更多開發者帶來便利!