iOS開屏廣告解決方案XHLaunchAd3.0

header.png

一.前言:

1.以前因爲項目須要,寫了一個開屏廣告組件XHLaunchAd,主要自用,後來發現愈來愈多的開發者,有這個需求.
2.隨着使用人數的增長,部分開發者使用者提出了一些需求和問題,XHLaunchAd並不能很好的解決.
3.因而決定最近抽時間,把這個組件優化重構一下,解決網友以前提出一些問題和需求,並增長一些新功能,發佈v3.0.0版本.
4.該項目目前已經收到 700 多個 star ,目前已經更新到v3.0.1版本
5.代碼地址: github.com/CoderZhuXH/…git

-> 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等等github

三.接入後效果:

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 擁有更靈活的調用方式和更優雅的接口,後期我還會持續維護並更新他,但願能給更多開發者帶來便利!服務器

相關文章
相關標籤/搜索