diff --git a/XHLaunchAd.podspec b/XHLaunchAd.podspec index 9481268..27cb01e 100755 --- a/XHLaunchAd.podspec +++ b/XHLaunchAd.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "XHLaunchAd" - s.version = "3.8.0" + s.version = "3.8.1" s.summary = "The screen opening advertising solutions - 开屏广告、启动广告解决方案,支持图片/视频、静态/动态、全屏/半屏广告,支持iPhone/iPad,自带图片下载、缓存功能,等等." s.homepage = "https://github.com/CoderZhuXH/XHLaunchAd" s.license = { :type => "MIT", :file => "LICENSE" } diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAd.h b/XHLaunchAd/XHLaunchAd/XHLaunchAd.h index 5ea367a..a9fe714 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAd.h +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAd.h @@ -6,8 +6,8 @@ // Copyright © 2016年 it7090.com. All rights reserved. // 代码地址:https://github.com/CoderZhuXH/XHLaunchAd -// 版本:3.8.0 -// 发布:2017.10.18 +// 版本:3.8.1 +// 发布:2017.10.20 // 如果你在使用过程中出现bug,请及时以下面任意一种方式联系我,我会及时修复bug并帮您解决问题。 // QQ交流群:537476189 @@ -25,9 +25,7 @@ NS_ASSUME_NONNULL_BEGIN @class XHLaunchAd; - @protocol XHLaunchAdDelegate - @optional /** @@ -89,7 +87,7 @@ NS_ASSUME_NONNULL_BEGIN -(void)xhLaunchAdShowFinish:(XHLaunchAd *)launchAd; /** - 如果你想用SDWebImage等框架加载网络广告图片,请实现此代理 + 如果你想用SDWebImage等框架加载网络广告图片,请实现此代理,注意:实现此方法后,图片缓存将不受XHLaunchAd管理 @param launchAd XHLaunchAd @param launchAdImageView launchAdImageView diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAd.m b/XHLaunchAd/XHLaunchAd/XHLaunchAd.m index 89d995d..31aba26 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAd.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAd.m @@ -15,7 +15,6 @@ #import "XHLaunchAdController.h" typedef NS_ENUM(NSInteger, XHLaunchAdType) { - XHLaunchAdTypeImage, XHLaunchAdTypeVideo }; @@ -40,170 +39,142 @@ @interface XHLaunchAd() @implementation XHLaunchAd -+(void)setWaitDataDuration:(NSInteger )waitDataDuration -{ ++(void)setWaitDataDuration:(NSInteger )waitDataDuration{ XHLaunchAd *launchAd = [XHLaunchAd shareLaunchAd]; launchAd.waitDataDuration = waitDataDuration; } +(void)setLaunchImagesSource:(LaunchImagesSource)launchImagesSource{ - _launchImagesSource = launchImagesSource; } -+(XHLaunchAd *)imageAdWithImageAdConfiguration:(XHLaunchImageAdConfiguration *)imageAdconfiguration -{ + ++(XHLaunchAd *)imageAdWithImageAdConfiguration:(XHLaunchImageAdConfiguration *)imageAdconfiguration{ return [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:nil]; } -+(XHLaunchAd *)imageAdWithImageAdConfiguration:(XHLaunchImageAdConfiguration *)imageAdconfiguration delegate:(id)delegate; -{ ++(XHLaunchAd *)imageAdWithImageAdConfiguration:(XHLaunchImageAdConfiguration *)imageAdconfiguration delegate:(id)delegate{ XHLaunchAd *launchAd = [XHLaunchAd shareLaunchAd]; if(delegate) launchAd.delegate = delegate; launchAd.imageAdConfiguration = imageAdconfiguration; return launchAd; } -+(XHLaunchAd *)videoAdWithVideoAdConfiguration:(XHLaunchVideoAdConfiguration *)videoAdconfiguration -{ + ++(XHLaunchAd *)videoAdWithVideoAdConfiguration:(XHLaunchVideoAdConfiguration *)videoAdconfiguration{ return [XHLaunchAd videoAdWithVideoAdConfiguration:videoAdconfiguration delegate:nil]; } -+(XHLaunchAd *)videoAdWithVideoAdConfiguration:(XHLaunchVideoAdConfiguration *)videoAdconfiguration delegate:(nullable id)delegate; -{ + ++(XHLaunchAd *)videoAdWithVideoAdConfiguration:(XHLaunchVideoAdConfiguration *)videoAdconfiguration delegate:(nullable id)delegate{ XHLaunchAd *launchAd = [XHLaunchAd shareLaunchAd]; if(delegate) launchAd.delegate = delegate; launchAd.videoAdConfiguration = videoAdconfiguration; return launchAd; } -+(void)downLoadImageAndCacheWithURLArray:(NSArray * )urlArray -{ + ++(void)downLoadImageAndCacheWithURLArray:(NSArray * )urlArray{ [self downLoadImageAndCacheWithURLArray:urlArray completed:nil]; } -+ (void)downLoadImageAndCacheWithURLArray:(NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock -{ + ++ (void)downLoadImageAndCacheWithURLArray:(NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock{ if(urlArray.count==0) return; [[XHLaunchAdDownloader sharedDownloader] downLoadImageAndCacheWithURLArray:urlArray completed:completedBlock]; } -+(void)downLoadVideoAndCacheWithURLArray:(NSArray * )urlArray -{ + ++(void)downLoadVideoAndCacheWithURLArray:(NSArray * )urlArray{ [self downLoadVideoAndCacheWithURLArray:urlArray completed:nil]; } -+(void)downLoadVideoAndCacheWithURLArray:(NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock -{ + ++(void)downLoadVideoAndCacheWithURLArray:(NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock{ if(urlArray.count==0) return; [[XHLaunchAdDownloader sharedDownloader] downLoadVideoAndCacheWithURLArray:urlArray completed:completedBlock]; } -+(void)skipAction -{ + ++(void)skipAction{ [[XHLaunchAd shareLaunchAd] removeAndAnimated:YES]; } -+(void)removeAndAnimated:(BOOL)animated -{ + ++(void)removeAndAnimated:(BOOL)animated{ [[XHLaunchAd shareLaunchAd] removeAndAnimated:animated]; } -+(BOOL)checkImageInCacheWithURL:(NSURL *)url -{ + ++(BOOL)checkImageInCacheWithURL:(NSURL *)url{ return [XHLaunchAdCache checkImageInCacheWithURL:url]; } -+(BOOL)checkVideoInCacheWithURL:(NSURL *)url -{ ++(BOOL)checkVideoInCacheWithURL:(NSURL *)url{ return [XHLaunchAdCache checkVideoInCacheWithURL:url]; } -+(void)clearDiskCache -{ ++(void)clearDiskCache{ [XHLaunchAdCache clearDiskCache]; } -+(void)clearDiskCacheWithImageUrlArray:(NSArray *)imageUrlArray -{ ++(void)clearDiskCacheWithImageUrlArray:(NSArray *)imageUrlArray{ [XHLaunchAdCache clearDiskCacheWithImageUrlArray:imageUrlArray]; } -+(void)clearDiskCacheExceptImageUrlArray:(NSArray *)exceptImageUrlArray -{ ++(void)clearDiskCacheExceptImageUrlArray:(NSArray *)exceptImageUrlArray{ [XHLaunchAdCache clearDiskCacheExceptImageUrlArray:exceptImageUrlArray]; } -+(void)clearDiskCacheWithVideoUrlArray:(NSArray *)videoUrlArray -{ ++(void)clearDiskCacheWithVideoUrlArray:(NSArray *)videoUrlArray{ [XHLaunchAdCache clearDiskCacheWithVideoUrlArray:videoUrlArray]; } -+(void)clearDiskCacheExceptVideoUrlArray:(NSArray *)exceptVideoUrlArray -{ ++(void)clearDiskCacheExceptVideoUrlArray:(NSArray *)exceptVideoUrlArray{ [XHLaunchAdCache clearDiskCacheExceptVideoUrlArray:exceptVideoUrlArray]; } -+(float)diskCacheSize -{ ++(float)diskCacheSize{ return [XHLaunchAdCache diskCacheSize]; } -+(NSString *)xhLaunchAdCachePath -{ + ++(NSString *)xhLaunchAdCachePath{ return [XHLaunchAdCache xhLaunchAdCachePath]; } -+(NSString *)cacheImageURLString -{ + ++(NSString *)cacheImageURLString{ return [XHLaunchAdCache getCacheImageUrl]; } -+(NSString *)cacheVideoURLString -{ + ++(NSString *)cacheVideoURLString{ return [XHLaunchAdCache getCacheVideoUrl]; } + #pragma mark - private +(XHLaunchAd *)shareLaunchAd{ - static XHLaunchAd *instance = nil; static dispatch_once_t oneToken; dispatch_once(&oneToken,^{ - instance = [[XHLaunchAd alloc] init]; }); return instance; - } -- (instancetype)init -{ +- (instancetype)init{ self = [super init]; if (self) { - [self setupLaunchAd]; - [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { - [self setupLaunchAdEnterForeground]; - }]; - - [[NSNotificationCenter defaultCenter] addObserverForName:XHLaunchAdDetailPageWillShowNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { - _detailPageShowing = YES; - }]; - [[NSNotificationCenter defaultCenter] addObserverForName:XHLaunchAdDetailPageShowFinishNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { - _detailPageShowing = NO; - }]; - } return self; } --(void)setupLaunchAdEnterForeground -{ +-(void)setupLaunchAdEnterForeground{ switch (_launchAdType) { - case XHLaunchAdTypeImage: - { + case XHLaunchAdTypeImage:{ if(!_imageAdConfiguration.showEnterForeground || _detailPageShowing) return; [self setupLaunchAd]; [self setupImageAdForConfiguration:_imageAdConfiguration]; } break; - case XHLaunchAdTypeVideo: - { + case XHLaunchAdTypeVideo:{ if(!_videoAdConfiguration.showEnterForeground || _detailPageShowing) return; [self setupLaunchAd]; [self setupVideoAdForConfiguration:_videoAdConfiguration]; @@ -213,8 +184,8 @@ -(void)setupLaunchAdEnterForeground break; } } --(void)setupLaunchAd -{ + +-(void)setupLaunchAd{ UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; window.rootViewController = [XHLaunchAdController new]; window.rootViewController.view.backgroundColor = [UIColor clearColor]; @@ -223,16 +194,13 @@ -(void)setupLaunchAd window.hidden = NO; window.alpha = 1; _window = window; - /** 添加launchImageView */ [_window addSubview:[[XHLaunchImageView alloc] initWithLaunchImagesSource:_launchImagesSource]]; } /**图片*/ --(void)setupImageAdForConfiguration:(XHLaunchImageAdConfiguration *)configuration -{ +-(void)setupImageAdForConfiguration:(XHLaunchImageAdConfiguration *)configuration{ [self removeSubViewsExceptLaunchAdImageView]; - XHLaunchAdImageView *adImageView = [[XHLaunchAdImageView alloc] init]; [_window addSubview:adImageView]; /** frame */ @@ -240,19 +208,14 @@ -(void)setupImageAdForConfiguration:(XHLaunchImageAdConfiguration *)configuratio if(configuration.contentMode) adImageView.contentMode = configuration.contentMode; /** webImage */ if(configuration.imageNameOrURLString.length && XHISURLString(configuration.imageNameOrURLString)){ - [XHLaunchAdCache async_saveImageUrl:configuration.imageNameOrURLString]; /** 自设图片 */ if ([self.delegate respondsToSelector:@selector(xhLaunchAd:launchAdImageView:URL:)]) { - [self.delegate xhLaunchAd:self launchAdImageView:adImageView URL:[NSURL URLWithString:configuration.imageNameOrURLString]]; - }else{ - if(!configuration.imageOption) configuration.imageOption = XHLaunchAdImageDefault; XHWeakSelf [adImageView xh_setImageWithURL:[NSURL URLWithString:configuration.imageNameOrURLString] placeholderImage:nil GIFImageCycleOnce:configuration.GIFImageCycleOnce options:configuration.imageOption completed:^(UIImage *image,NSData *imageData,NSError *error,NSURL *url){ - if ([weakSelf.delegate respondsToSelector:@selector(xhLaunchAd:imageDownLoadFinish:)]) { [weakSelf.delegate xhLaunchAd:self imageDownLoadFinish:image]; } @@ -260,7 +223,6 @@ -(void)setupImageAdForConfiguration:(XHLaunchImageAdConfiguration *)configuratio [weakSelf.delegate xhLaunchAd:self imageDownLoadFinish:image imageData:imageData]; } }]; - if(configuration.imageOption == XHLaunchAdImageCacheInBackground){ /** 缓存中未有 */ if(![XHLaunchAdCache checkImageInCacheWithURL:[NSURL URLWithString:configuration.imageNameOrURLString]]){ @@ -268,90 +230,59 @@ -(void)setupImageAdForConfiguration:(XHLaunchImageAdConfiguration *)configuratio } } } - }else{ - if(configuration.imageNameOrURLString.length){ - NSData *data = XHDataWithFileName(configuration.imageNameOrURLString); if(XHISGIFTypeWithData(data)){ - FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data]; adImageView.animatedImage = image; adImageView.image = nil; __weak typeof(adImageView) w_adImageView = adImageView; adImageView.loopCompletionBlock = ^(NSUInteger loopCountRemaining) { - if(configuration.GIFImageCycleOnce) [w_adImageView stopAnimating]; }; - }else{ - adImageView.animatedImage = nil; adImageView.image = [UIImage imageWithData:data]; - } - if ([self.delegate respondsToSelector:@selector(xhLaunchAd:imageDownLoadFinish:)]) { - [self.delegate xhLaunchAd:self imageDownLoadFinish:[UIImage imageWithData:data]]; } - }else{ - XHLaunchAdLog(@"未设置广告图片"); } - } - /** skipButton */ [self addSkipButtonForConfiguration:configuration]; - [self startSkipDispathTimer]; - /** customView */ if(configuration.subViews.count>0) [self addSubViews:configuration.subViews]; - XHWeakSelf adImageView.click = ^(){ - [weakSelf click]; }; - } --(void)addSkipButtonForConfiguration:(XHLaunchAdConfiguration *)configuration -{ + +-(void)addSkipButtonForConfiguration:(XHLaunchAdConfiguration *)configuration{ if(!configuration.duration) configuration.duration = 5; if(!configuration.skipButtonType) configuration.skipButtonType = SkipTypeTimeText; - if(configuration.customSkipView){ - [_window addSubview:configuration.customSkipView]; - }else{ - if(_skipButton == nil){ - _skipButton = [[XHLaunchAdButton alloc] initWithSkipType:configuration.skipButtonType]; _skipButton.hidden = YES; [_skipButton addTarget:self action:@selector(skipButtonClick) forControlEvents:UIControlEventTouchUpInside]; } - [_window addSubview:_skipButton]; [_skipButton setTitleWithSkipType:configuration.skipButtonType duration:configuration.duration]; - } } /**视频*/ --(void)setupVideoAdForConfiguration:(XHLaunchVideoAdConfiguration *)configuration -{ +-(void)setupVideoAdForConfiguration:(XHLaunchVideoAdConfiguration *)configuration{ [self removeSubViewsExceptLaunchAdImageView]; - - if(_adVideoView == nil){ - - _adVideoView = [[XHLaunchAdVideoView alloc] init]; - } + if(_adVideoView == nil) _adVideoView = [[XHLaunchAdVideoView alloc] init]; [_window addSubview:_adVideoView]; /** frame */ if(configuration.frame.size.width>0&&configuration.frame.size.height>0) _adVideoView.frame = configuration.frame; @@ -359,42 +290,30 @@ -(void)setupVideoAdForConfiguration:(XHLaunchVideoAdConfiguration *)configuratio _adVideoView.videoCycleOnce = configuration.videoCycleOnce; /** video 数据源 */ if(configuration.videoNameOrURLString.length && XHISURLString(configuration.videoNameOrURLString)){ - [XHLaunchAdCache async_saveVideoUrl:configuration.videoNameOrURLString]; - NSURL *pathURL = [XHLaunchAdCache getCacheVideoWithURL:[NSURL URLWithString:configuration.videoNameOrURLString]]; if(pathURL){ - if ([self.delegate respondsToSelector:@selector(xhLaunchAd:videoDownLoadFinish:)]) { [self.delegate xhLaunchAd:self videoDownLoadFinish:pathURL]; } _adVideoView.videoPlayer.contentURL = pathURL; [_adVideoView.videoPlayer prepareToPlay]; - }else{ - XHWeakSelf [[XHLaunchAdDownloader sharedDownloader] downloadVideoWithURL:[NSURL URLWithString:configuration.videoNameOrURLString] progress:^(unsigned long long total, unsigned long long current) { - if ([weakSelf.delegate respondsToSelector:@selector(xhLaunchAd:videoDownLoadProgress:total:current:)]) { [weakSelf.delegate xhLaunchAd:self videoDownLoadProgress:current/(float)total total:total current:current]; } - } completed:^(NSURL * _Nullable location, NSError * _Nullable error){ - if ([weakSelf.delegate respondsToSelector:@selector(xhLaunchAd:videoDownLoadFinish:)]){ [weakSelf.delegate xhLaunchAd:self videoDownLoadFinish:location]; } }]; - /***视频缓存,提前显示完成 */ [self removeAndAnimateDefault]; return; } - }else{ - if(configuration.videoNameOrURLString.length){ - NSString *path = [[NSBundle mainBundle]pathForResource:configuration.videoNameOrURLString ofType:nil]; if(path.length){ NSURL *pathURL = [NSURL fileURLWithPath:path]; @@ -403,90 +322,72 @@ -(void)setupVideoAdForConfiguration:(XHLaunchVideoAdConfiguration *)configuratio } _adVideoView.videoPlayer.contentURL = pathURL;; [_adVideoView.videoPlayer prepareToPlay]; - }else{ - XHLaunchAdLog(@"Error:广告视频未找到,请检查名称是否有误!"); } }else{ - XHLaunchAdLog(@"未设置广告视频"); } } - /** skipButton */ [self addSkipButtonForConfiguration:configuration]; - [self startSkipDispathTimer]; - /** customView */ if(configuration.subViews.count>0) [self addSubViews:configuration.subViews]; - XHWeakSelf _adVideoView.click = ^(){ - [weakSelf click]; }; } + #pragma mark - add subViews --(void)addSubViews:(NSArray *)subViews -{ +-(void)addSubViews:(NSArray *)subViews{ [subViews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) { - [_window addSubview:view]; }]; } + #pragma mark - set --(void)setImageAdConfiguration:(XHLaunchImageAdConfiguration *)imageAdConfiguration -{ +-(void)setImageAdConfiguration:(XHLaunchImageAdConfiguration *)imageAdConfiguration{ _imageAdConfiguration = imageAdConfiguration; _launchAdType = XHLaunchAdTypeImage; [self setupImageAdForConfiguration:imageAdConfiguration]; } --(void)setVideoAdConfiguration:(XHLaunchVideoAdConfiguration *)videoAdConfiguration -{ + +-(void)setVideoAdConfiguration:(XHLaunchVideoAdConfiguration *)videoAdConfiguration{ _videoAdConfiguration = videoAdConfiguration; _launchAdType = XHLaunchAdTypeVideo; [self setupVideoAdForConfiguration:videoAdConfiguration]; } --(void)setWaitDataDuration:(NSInteger)waitDataDuration -{ + +-(void)setWaitDataDuration:(NSInteger)waitDataDuration{ _waitDataDuration = waitDataDuration; /** 数据等待 */ [self startWaitDataDispathTiemr]; } #pragma mark - Action --(void)skipButtonClick -{ +-(void)skipButtonClick{ [self removeAndAnimated:YES]; } --(void)removeAndAnimated:(BOOL)animated -{ + +-(void)removeAndAnimated:(BOOL)animated{ if(animated){ - [self removeAndAnimate]; - } - else{ - + }else{ [self removeAndAnimateDefault]; } } --(void)click -{ + +-(void)click{ XHLaunchAdConfiguration * configuration = [self commonConfiguration]; - if ([self.delegate respondsToSelector:@selector(xhLaunchAd:clickAndOpenURLString:)] && configuration.openURLString.length) { - [self.delegate xhLaunchAd:self clickAndOpenURLString:configuration.openURLString]; - [self removeAndAnimateDefault]; } - } --(XHLaunchAdConfiguration *)commonConfiguration -{ +-(XHLaunchAdConfiguration *)commonConfiguration{ XHLaunchAdConfiguration *configuration = nil; switch (_launchAdType) { case XHLaunchAdTypeVideo: @@ -500,137 +401,110 @@ -(XHLaunchAdConfiguration *)commonConfiguration } return configuration; } --(void)startWaitDataDispathTiemr -{ + +-(void)startWaitDataDispathTiemr{ __block NSInteger duration = defaultWaitDataDuration; if(_waitDataDuration) duration = _waitDataDuration; _waitDataTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)); NSTimeInterval period = 1.0; dispatch_source_set_timer(_waitDataTimer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); dispatch_source_set_event_handler(_waitDataTimer, ^{ - dispatch_async(dispatch_get_main_queue(), ^{ if(duration==0){ - DISPATCH_SOURCE_CANCEL_SAFE(_waitDataTimer); [self removeAndAnimateDefault]; return ; } - duration--; }); }); - dispatch_resume(_waitDataTimer); } --(void)startSkipDispathTimer -{ +-(void)startSkipDispathTimer{ XHLaunchAdConfiguration * configuration = [self commonConfiguration]; DISPATCH_SOURCE_CANCEL_SAFE(_waitDataTimer); if(!configuration.skipButtonType) configuration.skipButtonType = SkipTypeTimeText;//默认 __block NSInteger duration = 5;//默认 if(configuration.duration) duration = configuration.duration; - if(configuration.skipButtonType == SkipTypeRoundProgressTime || configuration.skipButtonType == SkipTypeRoundProgressText) - { + if(configuration.skipButtonType == SkipTypeRoundProgressTime || configuration.skipButtonType == SkipTypeRoundProgressText){ [_skipButton startRoundDispathTimerWithDuration:duration]; } NSTimeInterval period = 1.0; _skipTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)); dispatch_source_set_timer(_skipTimer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); dispatch_source_set_event_handler(_skipTimer, ^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if ([self.delegate respondsToSelector:@selector(xhLaunchAd:customSkipView:duration:)]) { - [self.delegate xhLaunchAd:self customSkipView:configuration.customSkipView duration:duration]; } if(!configuration.customSkipView){ - [_skipButton setTitleWithSkipType:configuration.skipButtonType duration:duration]; } if(duration==0){ - DISPATCH_SOURCE_CANCEL_SAFE(_skipTimer); [self removeAndAnimate]; return ; } - duration--; }); }); - dispatch_resume(_skipTimer); } -(void)removeAndAnimate{ XHLaunchAdConfiguration * configuration = [self commonConfiguration]; - CGFloat duration = showFinishAnimateTimeDefault; if(configuration.showFinishAnimateTime>0) duration = configuration.showFinishAnimateTime; - switch (configuration.showFinishAnimate) { - case ShowFinishAnimateNone:{ - [self remove]; } break; case ShowFinishAnimateFadein:{ - [self removeAndAnimateDefault]; } break; case ShowFinishAnimateLite:{ - [UIView transitionWithView:_window duration:duration options:UIViewAnimationOptionCurveEaseOut animations:^{ _window.transform = CGAffineTransformMakeScale(1.5, 1.5); _window.alpha = 0; } completion:^(BOOL finished) { [self remove]; }]; - } break; case ShowFinishAnimateFlipFromLeft:{ - [UIView transitionWithView:_window duration:duration options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ _window.alpha = 0; } completion:^(BOOL finished) { [self remove]; }]; - } break; case ShowFinishAnimateFlipFromBottom:{ - [UIView transitionWithView:_window duration:duration options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{ _window.alpha = 0; } completion:^(BOOL finished) { [self remove]; }]; - } break; case ShowFinishAnimateCurlUp:{ - [UIView transitionWithView:_window duration:duration options:UIViewAnimationOptionTransitionCurlUp animations:^{ _window.alpha = 0; } completion:^(BOOL finished) { [self remove]; }]; - } break; default:{ - [self removeAndAnimateDefault]; } break; } } --(void)removeAndAnimateDefault -{ + +-(void)removeAndAnimateDefault{ XHLaunchAdConfiguration * configuration = [self commonConfiguration]; CGFloat duration = showFinishAnimateTimeDefault; if(configuration.showFinishAnimateTime>0) duration = configuration.showFinishAnimateTime; @@ -640,47 +514,35 @@ -(void)removeAndAnimateDefault [self remove]; }]; } + -(void)remove{ - DISPATCH_SOURCE_CANCEL_SAFE(_waitDataTimer) DISPATCH_SOURCE_CANCEL_SAFE(_skipTimer) REMOVE_FROM_SUPERVIEW_SAFE(_skipButton) if(_launchAdType==XHLaunchAdTypeVideo){ - if(_adVideoView==nil) return; [_adVideoView stopVideoPlayer]; REMOVE_FROM_SUPERVIEW_SAFE(_adVideoView) } - [_window.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - REMOVE_FROM_SUPERVIEW_SAFE(obj) }]; - _window.hidden = YES; _window = nil; - if ([self.delegate respondsToSelector:@selector(xhLaunchShowFinish:)]) { - [self.delegate xhLaunchShowFinish:self]; } - if ([self.delegate respondsToSelector:@selector(xhLaunchAdShowFinish:)]) { - [self.delegate xhLaunchAdShowFinish:self]; } - } --(void)removeSubViewsExceptLaunchAdImageView -{ +-(void)removeSubViewsExceptLaunchAdImageView{ [_window.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - if(![obj isKindOfClass:[XHLaunchImageView class]]){ - REMOVE_FROM_SUPERVIEW_SAFE(obj) } }]; - } + @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdButton.h b/XHLaunchAd/XHLaunchAd/XHLaunchAdButton.h index 4588311..ecb5af7 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdButton.h +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdButton.h @@ -12,14 +12,11 @@ * 倒计时类型 */ typedef NS_ENUM(NSInteger,SkipType) { - SkipTypeNone = 1,//无 - /** 方形 */ SkipTypeTime = 2,//方形:倒计时 SkipTypeText = 3,//方形:跳过 SkipTypeTimeText = 4,//方形:倒计时+跳过 (default) - /** 圆形 */ SkipTypeRoundTime = 5,//圆形:倒计时 SkipTypeRoundText = 6,//圆形:跳过 diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdButton.m b/XHLaunchAd/XHLaunchAd/XHLaunchAdButton.m index d104cb4..77c4f1f 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdButton.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdButton.m @@ -27,8 +27,7 @@ @interface XHLaunchAdButton() @implementation XHLaunchAdButton -- (instancetype)initWithSkipType:(SkipType)skipType -{ +- (instancetype)initWithSkipType:(SkipType)skipType{ self = [super init]; if (self) { @@ -36,66 +35,50 @@ - (instancetype)initWithSkipType:(SkipType)skipType CGFloat y = XH_IPHONEX ? 44 : 20; - /** 环形 */ - if(skipType == SkipTypeRoundTime || skipType ==SkipTypeRoundText || skipType == SkipTypeRoundProgressTime || skipType == SkipTypeRoundProgressText) - { + //环形 + if(skipType == SkipTypeRoundTime || skipType ==SkipTypeRoundText || skipType == SkipTypeRoundProgressTime || skipType == SkipTypeRoundProgressText){ self.frame = CGRectMake(XH_ScreenW-55,y, 42, 42); - } - /** 方形 */ - else - { + }else{//方形 self.frame = CGRectMake(XH_ScreenW-80,y, 70, 35); } - switch (skipType) { - case SkipTypeNone: - { + case SkipTypeNone:{ self.hidden = YES; } break; - case SkipTypeTime: - { + case SkipTypeTime:{ [self addSubview:self.timeLab]; self.leftRightSpace = 5; self.topBottomSpace = 2.5; - } break; - case SkipTypeText: - { + case SkipTypeText:{ [self addSubview:self.timeLab]; self.leftRightSpace = 5; self.topBottomSpace = 2.5; - } break; - case SkipTypeTimeText: - { + case SkipTypeTimeText:{ [self addSubview:self.timeLab]; self.leftRightSpace = 5; self.topBottomSpace = 2.5; - } break; - case SkipTypeRoundTime: - { + case SkipTypeRoundTime:{ [self addSubview:self.timeLab]; } break; - case SkipTypeRoundText: - { + case SkipTypeRoundText:{ [self addSubview:self.timeLab]; } break; - case SkipTypeRoundProgressTime: - { + case SkipTypeRoundProgressTime:{ [self addSubview:self.timeLab]; self.timeLab.backgroundColor = [UIColor clearColor]; [self.timeLab.layer addSublayer:self.roundLayer]; } break; - case SkipTypeRoundProgressText: - { + case SkipTypeRoundProgressText:{ [self addSubview:self.timeLab]; self.timeLab.backgroundColor = [UIColor clearColor]; [self.timeLab.layer addSublayer:self.roundLayer]; @@ -104,16 +87,12 @@ - (instancetype)initWithSkipType:(SkipType)skipType default: break; } - } - return self; } --(UILabel *)timeLab -{ - if(_timeLab == nil) - { +-(UILabel *)timeLab{ + if(_timeLab == nil){ _timeLab = [[UILabel alloc] initWithFrame:self.bounds]; _timeLab.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]; _timeLab.textColor = [UIColor whiteColor]; @@ -124,10 +103,9 @@ -(UILabel *)timeLab } return _timeLab; } --(CAShapeLayer *)roundLayer -{ - if(_roundLayer==nil) - { + +-(CAShapeLayer *)roundLayer{ + if(_roundLayer==nil){ _roundLayer = [CAShapeLayer layer]; _roundLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4].CGColor; _roundLayer.strokeColor = RoundProgressColor.CGColor; @@ -140,53 +118,45 @@ -(CAShapeLayer *)roundLayer } return _roundLayer; } -- (void)setTitleWithSkipType:(SkipType)skipType duration:(NSInteger)duration -{ + +- (void)setTitleWithSkipType:(SkipType)skipType duration:(NSInteger)duration{ switch (skipType) { - case SkipTypeNone: - { + case SkipTypeNone:{ self.hidden = YES; } break; - case SkipTypeTime: - { + case SkipTypeTime:{ self.hidden = NO; self.timeLab.text = [NSString stringWithFormat:@"%ld %@",duration,DurationUnit]; } break; - case SkipTypeText: - { + case SkipTypeText:{ self.hidden = NO; self.timeLab.text = SkipTitle; } break; - case SkipTypeTimeText: - { + case SkipTypeTimeText:{ self.hidden = NO; self.timeLab.text = [NSString stringWithFormat:@"%ld %@",duration,SkipTitle]; } break; - case SkipTypeRoundTime: - { + case SkipTypeRoundTime:{ self.hidden = NO; self.timeLab.text = [NSString stringWithFormat:@"%ld %@",duration,DurationUnit]; } break; - case SkipTypeRoundText: - { + case SkipTypeRoundText:{ self.hidden = NO; self.timeLab.text = SkipTitle; } break; - case SkipTypeRoundProgressTime: - { + case SkipTypeRoundProgressTime:{ self.hidden = NO; self.timeLab.text = [NSString stringWithFormat:@"%ld %@",duration,DurationUnit]; } break; - case SkipTypeRoundProgressText: - { + case SkipTypeRoundProgressText:{ self.hidden = NO; self.timeLab.text = SkipTitle; } @@ -196,18 +166,14 @@ - (void)setTitleWithSkipType:(SkipType)skipType duration:(NSInteger)duration } } --(void)startRoundDispathTimerWithDuration:(CGFloat )duration -{ +-(void)startRoundDispathTimerWithDuration:(CGFloat )duration{ NSTimeInterval period = 0.05; __block CGFloat roundDuration = duration; _roundTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)); dispatch_source_set_timer(_roundTimer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); dispatch_source_set_event_handler(_roundTimer, ^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if(roundDuration<=0){ - self.roundLayer.strokeStart = 1; DISPATCH_SOURCE_CANCEL_SAFE(_roundTimer); } @@ -215,12 +181,10 @@ -(void)startRoundDispathTimerWithDuration:(CGFloat )duration roundDuration -= period; }); }); - dispatch_resume(_roundTimer); } --(void)setLeftRightSpace:(CGFloat)leftRightSpace -{ +-(void)setLeftRightSpace:(CGFloat)leftRightSpace{ _leftRightSpace = leftRightSpace; CGRect frame = self.timeLab.frame; CGFloat width = frame.size.width; @@ -229,8 +193,8 @@ -(void)setLeftRightSpace:(CGFloat)leftRightSpace self.timeLab.frame = frame; [self cornerRadiusWithView:self.timeLab]; } --(void)setTopBottomSpace:(CGFloat)topBottomSpace -{ + +-(void)setTopBottomSpace:(CGFloat)topBottomSpace{ _topBottomSpace = topBottomSpace; CGRect frame = self.timeLab.frame; CGFloat height = frame.size.height; @@ -239,11 +203,10 @@ -(void)setTopBottomSpace:(CGFloat)topBottomSpace self.timeLab.frame = frame; [self cornerRadiusWithView:self.timeLab]; } --(void)cornerRadiusWithView:(UIView *)view -{ + +-(void)cornerRadiusWithView:(UIView *)view{ CGFloat min = view.frame.size.height; - if(view.frame.size.height > view.frame.size.width) - { + if(view.frame.size.height > view.frame.size.width) { min = view.frame.size.width; } view.layer.cornerRadius = min/2.0; diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdCache.h b/XHLaunchAd/XHLaunchAd/XHLaunchAdCache.h index ec51715..2f5f09e 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdCache.h +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdCache.h @@ -176,6 +176,8 @@ typedef void(^SaveCompletionBlock)(BOOL result , NSURL * URL); */ +(float)diskCacheSize; +#pragma mark - other + +(NSString *)md5String:(NSString *)string; @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdCache.m b/XHLaunchAd/XHLaunchAd/XHLaunchAdCache.m index 06c0aef..39b82b0 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdCache.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdCache.m @@ -12,100 +12,86 @@ @implementation XHLaunchAdCache -+(UIImage *)getCacheImageWithURL:(NSURL *)url -{ ++(UIImage *)getCacheImageWithURL:(NSURL *)url{ if(url==nil) return nil; NSData *data = [NSData dataWithContentsOfFile:[self imagePathWithURL:url]]; return [UIImage imageWithData:data]; } -+(NSData *)getCacheImageDataWithURL:(NSURL *)url -{ + ++(NSData *)getCacheImageDataWithURL:(NSURL *)url{ if(url==nil) return nil; return [NSData dataWithContentsOfFile:[self imagePathWithURL:url]]; } + +(BOOL)saveImageData:(NSData *)data imageURL:(NSURL *)url{ - NSString *path = [NSString stringWithFormat:@"%@/%@",[self xhLaunchAdCachePath],[self keyWithURL:url]]; if (data) { BOOL result = [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil]; - if (!result) XHLaunchAdLog(@"cache file error for URL: %@", url); - return result; } return NO; } -+(void)async_saveImageData:(NSData *)data imageURL:(NSURL *)url completed:(nullable SaveCompletionBlock)completedBlock -{ + ++(void)async_saveImageData:(NSData *)data imageURL:(NSURL *)url completed:(nullable SaveCompletionBlock)completedBlock{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - BOOL result = [self saveImageData:data imageURL:url]; dispatch_async(dispatch_get_main_queue(), ^{ - if(completedBlock) completedBlock(result , url); }); }); - } -+(BOOL)saveVideoAtLocation:(NSURL *)location URL:(NSURL *)url -{ ++(BOOL)saveVideoAtLocation:(NSURL *)location URL:(NSURL *)url{ NSString *savePath = [[self xhLaunchAdCachePath] stringByAppendingPathComponent:[self videoNameWithURL:url]]; NSURL *savePathUrl = [NSURL fileURLWithPath:savePath]; BOOL result =[[NSFileManager defaultManager] moveItemAtURL:location toURL:savePathUrl error:nil]; if(!result) XHLaunchAdLog(@"cache file error for URL: %@", url); return result; - } -+(void)async_saveVideoAtLocation:(NSURL *)location URL:(NSURL *)url completed:(nullable SaveCompletionBlock)completedBlock -{ + ++(void)async_saveVideoAtLocation:(NSURL *)location URL:(NSURL *)url completed:(nullable SaveCompletionBlock)completedBlock{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - BOOL result = [self saveVideoAtLocation:location URL:url]; - dispatch_async(dispatch_get_main_queue(), ^{ - if(completedBlock) completedBlock(result , url); }); }); - } -+(nullable NSURL *)getCacheVideoWithURL:(NSURL *)url -{ + ++(nullable NSURL *)getCacheVideoWithURL:(NSURL *)url{ NSString *savePath = [[self xhLaunchAdCachePath] stringByAppendingPathComponent:[self videoNameWithURL:url]]; //如果存在 - if([[NSFileManager defaultManager] fileExistsAtPath:savePath]) - { + if([[NSFileManager defaultManager] fileExistsAtPath:savePath]){ return [NSURL fileURLWithPath:savePath]; } return nil; } + + (NSString *)xhLaunchAdCachePath{ - NSString *path =[NSHomeDirectory() stringByAppendingPathComponent:@"Library/XHLaunchAdCache"]; [self checkDirectory:path]; return path; - } -+(NSString *)imagePathWithURL:(NSURL *)url -{ ++(NSString *)imagePathWithURL:(NSURL *)url{ if(url==nil) return nil; return [[self xhLaunchAdCachePath] stringByAppendingPathComponent:[self keyWithURL:url]]; } -+(NSString *)videoPathWithURL:(NSURL *)url -{ + ++(NSString *)videoPathWithURL:(NSURL *)url{ if(url==nil) return nil; return [[self xhLaunchAdCachePath] stringByAppendingPathComponent:[self videoNameWithURL:url]]; } -+(BOOL)checkImageInCacheWithURL:(NSURL *)url -{ + ++(BOOL)checkImageInCacheWithURL:(NSURL *)url{ return [[NSFileManager defaultManager] fileExistsAtPath:[self imagePathWithURL:url]]; } -+(BOOL)checkVideoInCacheWithURL:(NSURL *)url -{ + ++(BOOL)checkVideoInCacheWithURL:(NSURL *)url{ return [[NSFileManager defaultManager] fileExistsAtPath:[self videoPathWithURL:url]]; } + +(void)checkDirectory:(NSString *)path { NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isDir; @@ -119,136 +105,102 @@ +(void)checkDirectory:(NSString *)path { } } } + #pragma mark - url缓存 +(void)async_saveImageUrl:(NSString *)url{ - if(url==nil) return; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [[NSUserDefaults standardUserDefaults] setObject:url forKey:XHCacheImageUrlStringKey]; [[NSUserDefaults standardUserDefaults] synchronize]; }); - } -+(NSString *)getCacheImageUrl{ ++(NSString *)getCacheImageUrl{ return [[NSUserDefaults standardUserDefaults] objectForKey:XHCacheImageUrlStringKey]; } -+(void)async_saveVideoUrl:(NSString *)url{ ++(void)async_saveVideoUrl:(NSString *)url{ if(url==nil) return; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [[NSUserDefaults standardUserDefaults] setObject:url forKey:XHCacheVideoUrlStringKey]; [[NSUserDefaults standardUserDefaults] synchronize]; }); - } -+(NSString *)getCacheVideoUrl -{ + ++(NSString *)getCacheVideoUrl{ return [[NSUserDefaults standardUserDefaults] objectForKey:XHCacheVideoUrlStringKey]; } #pragma mark - 其他 -+(void)clearDiskCache -{ ++(void)clearDiskCache{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *path = [self xhLaunchAdCachePath]; [fileManager removeItemAtPath:path error:nil]; [self checkDirectory:[self xhLaunchAdCachePath]]; - }); - } -+(void)clearDiskCacheWithImageUrlArray:(NSArray *)imageUrlArray -{ + ++(void)clearDiskCacheWithImageUrlArray:(NSArray *)imageUrlArray{ if(imageUrlArray.count==0) return; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [imageUrlArray enumerateObjectsUsingBlock:^(NSURL * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - - if([self checkImageInCacheWithURL:obj]) - { + if([self checkImageInCacheWithURL:obj]){ [[NSFileManager defaultManager] removeItemAtPath:[self imagePathWithURL:obj] error:nil]; } }]; - }); } -+(void)clearDiskCacheExceptImageUrlArray:(NSArray *)exceptImageUrlArray -{ ++(void)clearDiskCacheExceptImageUrlArray:(NSArray *)exceptImageUrlArray{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSArray *allFilePaths = [self allFilePathWithDirectoryPath:[self xhLaunchAdCachePath]]; NSArray *exceptImagePaths = [self filePathsWithFileUrlArray:exceptImageUrlArray videoType:NO]; [allFilePaths enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - - if(![exceptImagePaths containsObject:obj] && !XHISVideoTypeWithPath(obj)) - { + if(![exceptImagePaths containsObject:obj] && !XHISVideoTypeWithPath(obj)){ [[NSFileManager defaultManager] removeItemAtPath:obj error:nil]; } }]; - XHLaunchAdLog(@"allFilePath = %@",allFilePaths); - }); } -+(void)clearDiskCacheWithVideoUrlArray:(NSArray *)videoUrlArray -{ ++(void)clearDiskCacheWithVideoUrlArray:(NSArray *)videoUrlArray{ if(videoUrlArray.count==0) return; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [videoUrlArray enumerateObjectsUsingBlock:^(NSURL * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - - if([self checkVideoInCacheWithURL:obj]) - { + if([self checkVideoInCacheWithURL:obj]){ [[NSFileManager defaultManager] removeItemAtPath:[self videoPathWithURL:obj] error:nil]; } }]; - }); } -+(void)clearDiskCacheExceptVideoUrlArray:(NSArray *)exceptVideoUrlArray -{ ++(void)clearDiskCacheExceptVideoUrlArray:(NSArray *)exceptVideoUrlArray{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSArray *allFilePaths = [self allFilePathWithDirectoryPath:[self xhLaunchAdCachePath]]; NSArray *exceptVideoPaths = [self filePathsWithFileUrlArray:exceptVideoUrlArray videoType:YES]; [allFilePaths enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - - if(![exceptVideoPaths containsObject:obj] && XHISVideoTypeWithPath(obj)) - { + if(![exceptVideoPaths containsObject:obj] && XHISVideoTypeWithPath(obj)){ [[NSFileManager defaultManager] removeItemAtPath:obj error:nil]; } }]; - XHLaunchAdLog(@"allFilePath = %@",allFilePaths); - }); } -+(float)diskCacheSize -{ + ++(float)diskCacheSize{ NSString *directoryPath = [self xhLaunchAdCachePath]; BOOL isDir = NO; unsigned long long total = 0; - if ([[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:&isDir]) { if (isDir) { NSError *error = nil; NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:&error]; - if (error == nil) { for (NSString *subpath in array) { NSString *path = [directoryPath stringByAppendingPathComponent:subpath]; - NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path - error:&error]; + NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error]; if (!error) { total += [dict[NSFileSize] unsignedIntegerValue]; } @@ -257,20 +209,15 @@ +(float)diskCacheSize } } return total/(1024.0*1024.0); - } -+(NSArray *)filePathsWithFileUrlArray:(NSArray *)fileUrlArray videoType:(BOOL)videoType -{ ++(NSArray *)filePathsWithFileUrlArray:(NSArray *)fileUrlArray videoType:(BOOL)videoType{ NSMutableArray *filePaths = [[NSMutableArray alloc] init]; [fileUrlArray enumerateObjectsUsingBlock:^(NSURL * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSString *path; - if(videoType) - { + if(videoType){ path = [self videoPathWithURL:obj]; - } - else - { + }else{ path = [self imagePathWithURL:obj]; } [filePaths addObject:path]; @@ -279,23 +226,14 @@ +(NSArray *)filePathsWithFileUrlArray:(NSArray *)fileUrlArray videoTyp } +(NSArray*)allFilePathWithDirectoryPath:(NSString*)directoryPath{ - NSMutableArray* array = [[NSMutableArray alloc] init]; - NSFileManager* fileManager = [NSFileManager defaultManager]; - NSArray* tempArray = [fileManager contentsOfDirectoryAtPath:directoryPath error:nil]; - for (NSString* fileName in tempArray) { - BOOL flag = YES; - NSString* fullPath = [directoryPath stringByAppendingPathComponent:fileName]; - if ([fileManager fileExistsAtPath:fullPath isDirectory:&flag]) { - if (!flag) { - [array addObject:fullPath]; } } @@ -305,8 +243,7 @@ +(NSArray*)allFilePathWithDirectoryPath:(NSString*)directoryPath{ + (void)createBaseDirectoryAtPath:(NSString *)path { __autoreleasing NSError *error = nil; - [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES - attributes:nil error:&error]; + [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]; if (error) { XHLaunchAdLog(@"create cache directory failed, error = %@", error); } else { @@ -323,24 +260,23 @@ + (void)addDoNotBackupAttribute:(NSString *)path { } } -+(NSString *)md5String:(NSString *)string -{ ++(NSString *)md5String:(NSString *)string{ const char *value = [string UTF8String]; unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH]; CC_MD5(value, (CC_LONG)strlen(value), outputBuffer); - NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){ [outputString appendFormat:@"%02x",outputBuffer[count]]; } return outputString; } -+(NSString *)videoNameWithURL:(NSURL *)url -{ + ++(NSString *)videoNameWithURL:(NSURL *)url{ return [[self md5String:url.absoluteString] stringByAppendingString:@".mp4"]; } -+(NSString *)keyWithURL:(NSURL *)url -{ + ++(NSString *)keyWithURL:(NSURL *)url{ return [self md5String:url.absoluteString]; } + @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdConfiguration.h b/XHLaunchAd/XHLaunchAd/XHLaunchAdConfiguration.h index c600578..874d3bc 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdConfiguration.h +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdConfiguration.h @@ -19,22 +19,16 @@ static CGFloat const showFinishAnimateTimeDefault = 0.8; /** 显示完成动画类型 */ typedef NS_ENUM(NSInteger , ShowFinishAnimate) { - /** 无动画 */ ShowFinishAnimateNone = 1, - /** 普通淡入(default) */ ShowFinishAnimateFadein = 2, - /** 放大淡入 */ ShowFinishAnimateLite = 3, - /** 左右翻转(类似网易云音乐) */ ShowFinishAnimateFlipFromLeft = 4, - /** 下上翻转 */ ShowFinishAnimateFlipFromBottom = 5, - /** 向上翻页 */ ShowFinishAnimateCurlUp = 6, }; diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdConfiguration.m b/XHLaunchAd/XHLaunchAd/XHLaunchAdConfiguration.m index e64fce7..b38b0ee 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdConfiguration.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdConfiguration.m @@ -8,7 +8,6 @@ #import "XHLaunchAdConfiguration.h" - #pragma mark - 公共 @implementation XHLaunchAdConfiguration @@ -17,8 +16,7 @@ @implementation XHLaunchAdConfiguration #pragma mark - 图片广告相关 @implementation XHLaunchImageAdConfiguration -+(XHLaunchImageAdConfiguration *)defaultConfiguration -{ ++(XHLaunchImageAdConfiguration *)defaultConfiguration{ //配置广告数据 XHLaunchImageAdConfiguration *configuration = [XHLaunchImageAdConfiguration new]; //广告停留时间 @@ -41,12 +39,12 @@ +(XHLaunchImageAdConfiguration *)defaultConfiguration configuration.showEnterForeground = NO; return configuration; } + @end #pragma mark - 视频广告相关 @implementation XHLaunchVideoAdConfiguration -+(XHLaunchVideoAdConfiguration *)defaultConfiguration -{ ++(XHLaunchVideoAdConfiguration *)defaultConfiguration{ //配置广告数据 XHLaunchVideoAdConfiguration *configuration = [XHLaunchVideoAdConfiguration new]; //广告停留时间 diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdController.m b/XHLaunchAd/XHLaunchAd/XHLaunchAdController.m index 6711e1e..3fca653 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdController.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdController.m @@ -14,9 +14,7 @@ @interface XHLaunchAdController () @implementation XHLaunchAdController --(BOOL)shouldAutorotate -{ +-(BOOL)shouldAutorotate{ return NO; } - @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdDownloader.h b/XHLaunchAd/XHLaunchAd/XHLaunchAdDownloader.h index 5f43184..471b741 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdDownloader.h +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdDownloader.h @@ -10,15 +10,12 @@ #import #pragma mark - XHLaunchAdDownload -typedef void(^XHLaunchAdDownloadProgressBlock)(unsigned long long total, unsigned long long current); +typedef void(^XHLaunchAdDownloadProgressBlock)(unsigned long long total, unsigned long long current); typedef void(^XHLaunchAdDownloadImageCompletedBlock)(UIImage *_Nullable image, NSData * _Nullable data, NSError * _Nullable error); - typedef void(^XHLaunchAdDownloadVideoCompletedBlock)(NSURL * _Nullable location, NSError * _Nullable error); - typedef void(^XHLaunchAdBatchDownLoadAndCacheCompletedBlock) (NSArray * _Nonnull completedArray); - @protocol XHLaunchAdDownloadDelegate - (void)downloadFinishWithURL:(nonnull NSURL *)url; @@ -26,9 +23,7 @@ typedef void(^XHLaunchAdBatchDownLoadAndCacheCompletedBlock) (NSArray * _Nonnull @end @interface XHLaunchAdDownload : NSObject - @property (assign, nonatomic ,nonnull)id delegate; - @end @interface XHLaunchAdImageDownload : XHLaunchAdDownload diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdDownloader.m b/XHLaunchAd/XHLaunchAd/XHLaunchAdDownloader.m index 29d7804..121f316 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdDownloader.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdDownloader.m @@ -13,8 +13,6 @@ #pragma mark - XHLaunchAdDownload - - @interface XHLaunchAdDownload() @property (strong, nonatomic) NSURLSession *session; @@ -27,7 +25,6 @@ @interface XHLaunchAdDownload() @end @implementation XHLaunchAdDownload - @end #pragma mark - XHLaunchAdImageDownload @@ -38,10 +35,8 @@ @interface XHLaunchAdImageDownload() @property (strong, nonatomic, nonnull) NSOperationQueue *downloadImageQueue; @@ -216,15 +187,12 @@ +(nonnull instancetype )sharedDownloader{ static XHLaunchAdDownloader *instance = nil; static dispatch_once_t oneToken; dispatch_once(&oneToken,^{ - instance = [[XHLaunchAdDownloader alloc] init]; - }); return instance; } -- (instancetype)init -{ +- (instancetype)init{ self = [super init]; if (self) { _downloadImageQueue = [NSOperationQueue new]; @@ -233,152 +201,121 @@ - (instancetype)init _downloadVideoQueue = [NSOperationQueue new]; _downloadVideoQueue.maxConcurrentOperationCount = 3; _downloadVideoQueue.name = @"com.it7090.XHLaunchAdDownloadVideoQueue"; - XHLaunchAdLog(@"XHLaunchAdCachePath:%@",[XHLaunchAdCache xhLaunchAdCachePath]); } return self; } -- (void)downloadImageWithURL:(nonnull NSURL *)url progress:(nullable XHLaunchAdDownloadProgressBlock)progressBlock completed:(nullable XHLaunchAdDownloadImageCompletedBlock)completedBlock -{ +- (void)downloadImageWithURL:(nonnull NSURL *)url progress:(nullable XHLaunchAdDownloadProgressBlock)progressBlock completed:(nullable XHLaunchAdDownloadImageCompletedBlock)completedBlock{ NSString *key = [self keyWithURL:url]; if(self.allDownloadDict[key]) return; XHLaunchAdImageDownload * download = [[XHLaunchAdImageDownload alloc] initWithURL:url delegateQueue:_downloadImageQueue progress:progressBlock completed:completedBlock]; download.delegate = self; [self.allDownloadDict setObject:download forKey:key]; } -- (void)downloadImageAndCacheWithURL:(nonnull NSURL *)url completed:(void(^)(BOOL result))completedBlock -{ + +- (void)downloadImageAndCacheWithURL:(nonnull NSURL *)url completed:(void(^)(BOOL result))completedBlock{ if(url == nil){ if(completedBlock) completedBlock(NO); - return; + return; } - [self downloadImageWithURL:url progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error) { - if(error){ if(completedBlock) completedBlock(NO); }else{ - [XHLaunchAdCache async_saveImageData:data imageURL:url completed:^(BOOL result, NSURL * _Nonnull URL) { - if(completedBlock) completedBlock(result); }]; } }]; } --(void)downLoadImageAndCacheWithURLArray:(NSArray *)urlArray -{ + +-(void)downLoadImageAndCacheWithURLArray:(NSArray *)urlArray{ [self downLoadImageAndCacheWithURLArray:urlArray completed:nil]; } -- (void)downLoadImageAndCacheWithURLArray:(nonnull NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock -{ + +- (void)downLoadImageAndCacheWithURLArray:(nonnull NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock{ if(urlArray.count==0) return; __block NSMutableArray * resultArray = [[NSMutableArray alloc] init]; dispatch_group_t downLoadGroup = dispatch_group_create(); [urlArray enumerateObjectsUsingBlock:^(NSURL *url, NSUInteger idx, BOOL *stop) { - if(![XHLaunchAdCache checkImageInCacheWithURL:url]){ - dispatch_group_enter(downLoadGroup); - [self downloadImageAndCacheWithURL:url completed:^(BOOL result) { - dispatch_group_leave(downLoadGroup); [resultArray addObject:@{@"url":url.absoluteString,@"result":@(result)}]; }]; - }else{ - [resultArray addObject:@{@"url":url.absoluteString,@"result":@(YES)}]; } }]; - dispatch_group_notify(downLoadGroup, dispatch_get_main_queue(), ^{ - if(completedBlock) completedBlock(resultArray); }); - } -- (void)downloadVideoWithURL:(nonnull NSURL *)url progress:(nullable XHLaunchAdDownloadProgressBlock)progressBlock completed:(nullable XHLaunchAdDownloadVideoCompletedBlock)completedBlock -{ + +- (void)downloadVideoWithURL:(nonnull NSURL *)url progress:(nullable XHLaunchAdDownloadProgressBlock)progressBlock completed:(nullable XHLaunchAdDownloadVideoCompletedBlock)completedBlock{ NSString *key = [self keyWithURL:url]; if(self.allDownloadDict[key]) return; - XHLaunchAdVideoDownload * download = [[XHLaunchAdVideoDownload alloc] initWithURL:url delegateQueue:_downloadVideoQueue progress:progressBlock completed:completedBlock]; download.delegate = self; [self.allDownloadDict setObject:download forKey:key]; } -- (void)downloadVideoAndCacheWithURL:(nonnull NSURL *)url completed:(void(^)(BOOL result))completedBlock -{ + +- (void)downloadVideoAndCacheWithURL:(nonnull NSURL *)url completed:(void(^)(BOOL result))completedBlock{ if(url == nil){ if(completedBlock) completedBlock(NO); return; } - [self downloadVideoWithURL:url progress:nil completed:^(NSURL * _Nullable location, NSError * _Nullable error) { - if(error){ - if(completedBlock) completedBlock(NO); - } - else{ - + }else{ [XHLaunchAdCache async_saveVideoAtLocation:location URL:url completed:^(BOOL result, NSURL * _Nonnull URL) { - if(completedBlock) completedBlock(result); }]; } }]; } -- (void)downLoadVideoAndCacheWithURLArray:(nonnull NSArray * )urlArray -{ + +- (void)downLoadVideoAndCacheWithURLArray:(nonnull NSArray * )urlArray{ [self downLoadVideoAndCacheWithURLArray:urlArray completed:nil]; } -- (void)downLoadVideoAndCacheWithURLArray:(nonnull NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock -{ + +- (void)downLoadVideoAndCacheWithURLArray:(nonnull NSArray * )urlArray completed:(nullable XHLaunchAdBatchDownLoadAndCacheCompletedBlock)completedBlock{ if(urlArray.count==0) return; __block NSMutableArray * resultArray = [[NSMutableArray alloc] init]; dispatch_group_t downLoadGroup = dispatch_group_create(); [urlArray enumerateObjectsUsingBlock:^(NSURL *url, NSUInteger idx, BOOL *stop) { - - if(![XHLaunchAdCache checkVideoInCacheWithURL:url]) - { + if(![XHLaunchAdCache checkVideoInCacheWithURL:url]){ dispatch_group_enter(downLoadGroup); - [self downloadVideoAndCacheWithURL:url completed:^(BOOL result) { - dispatch_group_leave(downLoadGroup); [resultArray addObject:@{@"url":url.absoluteString,@"result":@(result)}]; }]; - }else{ - [resultArray addObject:@{@"url":url.absoluteString,@"result":@(YES)}]; } }]; - dispatch_group_notify(downLoadGroup, dispatch_get_main_queue(), ^{ - if(completedBlock) completedBlock(resultArray); }); - } - (NSMutableDictionary *)allDownloadDict { if (!_allDownloadDict) { _allDownloadDict = [[NSMutableDictionary alloc] init]; } - return _allDownloadDict; } + - (void)downloadFinishWithURL:(NSURL *)url{ - [self.allDownloadDict removeObjectForKey:[self keyWithURL:url]]; } --(NSString *)keyWithURL:(NSURL *)url -{ + +-(NSString *)keyWithURL:(NSURL *)url{ return [XHLaunchAdCache md5String:url.absoluteString]; } + @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdImageManager.h b/XHLaunchAd/XHLaunchAd/XHLaunchAdImageManager.h index 96b6bf5..1a1e66f 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdImageManager.h +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdImageManager.h @@ -10,29 +10,15 @@ #import #import "XHLaunchAdDownloader.h" - typedef NS_OPTIONS(NSUInteger, XHLaunchAdImageOptions) { - - /** - * 有缓存,读取缓存,不重新下载,没缓存先下载,并缓存 - */ + /** 有缓存,读取缓存,不重新下载,没缓存先下载,并缓存 */ XHLaunchAdImageDefault = 1 << 0, - - /** - * 只下载,不缓存 - */ + /** 只下载,不缓存 */ XHLaunchAdImageOnlyLoad = 1 << 1, - - /** - * 先读缓存,再下载刷新图片和缓存 - */ + /** 先读缓存,再下载刷新图片和缓存 */ XHLaunchAdImageRefreshCached = 1 << 2 , - - /** - * 后台缓存本次不显示,缓存OK后下次再显示(建议使用这种方式) - */ + /** 后台缓存本次不显示,缓存OK后下次再显示(建议使用这种方式)*/ XHLaunchAdImageCacheInBackground = 1 << 3 - }; typedef void(^XHExternalCompletionBlock)(UIImage * _Nullable image,NSData * _Nullable imageData, NSError * _Nullable error, NSURL * _Nullable imageURL); @@ -40,7 +26,6 @@ typedef void(^XHExternalCompletionBlock)(UIImage * _Nullable image,NSData * _Nul @interface XHLaunchAdImageManager : NSObject +(nonnull instancetype )sharedManager; - - (void)loadImageWithURL:(nullable NSURL *)url options:(XHLaunchAdImageOptions)options progress:(nullable XHLaunchAdDownloadProgressBlock)progressBlock completed:(nullable XHExternalCompletionBlock)completedBlock; @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdImageManager.m b/XHLaunchAd/XHLaunchAd/XHLaunchAdImageManager.m index 0de8e96..29384c7 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdImageManager.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdImageManager.m @@ -17,88 +17,59 @@ @interface XHLaunchAdImageManager() @implementation XHLaunchAdImageManager +(nonnull instancetype )sharedManager{ - static XHLaunchAdImageManager *instance = nil; static dispatch_once_t oneToken; dispatch_once(&oneToken,^{ - instance = [[XHLaunchAdImageManager alloc] init]; }); return instance; } -- (instancetype)init -{ + +- (instancetype)init{ self = [super init]; if (self) { - _downloader = [XHLaunchAdDownloader sharedDownloader]; } return self; } -- (void)loadImageWithURL:(nullable NSURL *)url options:(XHLaunchAdImageOptions)options progress:(nullable XHLaunchAdDownloadProgressBlock)progressBlock completed:(nullable XHExternalCompletionBlock)completedBlock -{ + +- (void)loadImageWithURL:(nullable NSURL *)url options:(XHLaunchAdImageOptions)options progress:(nullable XHLaunchAdDownloadProgressBlock)progressBlock completed:(nullable XHExternalCompletionBlock)completedBlock{ if(!options) options = XHLaunchAdImageDefault; - if(options & XHLaunchAdImageOnlyLoad) - { - + if(options & XHLaunchAdImageOnlyLoad){ [_downloader downloadImageWithURL:url progress:progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error) { - if(completedBlock) completedBlock(image,data,error,url); - }]; - } - else if (options & XHLaunchAdImageRefreshCached) - { - + }else if (options & XHLaunchAdImageRefreshCached){ NSData *imageData = [XHLaunchAdCache getCacheImageDataWithURL:url]; UIImage *image = [UIImage imageWithData:imageData]; if(image && completedBlock) completedBlock(image,imageData,nil,url); [_downloader downloadImageWithURL:url progress:progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error) { - if(completedBlock) completedBlock(image,data,error,url); [XHLaunchAdCache async_saveImageData:data imageURL:url completed:nil]; - }]; - - } - else if (options & XHLaunchAdImageCacheInBackground) - { + }else if (options & XHLaunchAdImageCacheInBackground){ NSData *imageData = [XHLaunchAdCache getCacheImageDataWithURL:url]; UIImage *image = [UIImage imageWithData:imageData]; - if(image && completedBlock) - { + if(image && completedBlock){ completedBlock(image,imageData,nil,url); - } - else - { + }else{ [_downloader downloadImageWithURL:url progress:progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error) { - [XHLaunchAdCache async_saveImageData:data imageURL:url completed:nil]; - }]; } - - } - else//default - { + }else{//default NSData *imageData = [XHLaunchAdCache getCacheImageDataWithURL:url]; UIImage *image = [UIImage imageWithData:imageData]; - if(image && completedBlock) - { + if(image && completedBlock){ completedBlock(image,imageData,nil,url); - } - else - { + }else{ [_downloader downloadImageWithURL:url progress:progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error) { - if(completedBlock) completedBlock(image,data,error,url); - [XHLaunchAdCache async_saveImageData:data imageURL:url completed:nil]; - }]; } - } } + @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdImageView+XHLaunchAdCache.h b/XHLaunchAd/XHLaunchAd/XHLaunchAdImageView+XHLaunchAdCache.h index 1d4f08a..79db072 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdImageView+XHLaunchAdCache.h +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdImageView+XHLaunchAdCache.h @@ -11,18 +11,67 @@ @interface XHLaunchAdImageView (XHLaunchAdCache) +/** + 设置url图片 + + @param url 图片url + */ - (void)xh_setImageWithURL:(nonnull NSURL *)url; +/** + 设置url图片 + + @param url 图片url + @param placeholder 占位图 + */ - (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder; +/** + 设置url图片 + + @param url 图片url + @param placeholder 占位图 + @param options XHLaunchAdImageOptions + */ - (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(XHLaunchAdImageOptions)options; +/** + 设置url图片 + + @param url 图片url + @param placeholder 占位图 + @param completedBlock XHExternalCompletionBlock + */ - (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable XHExternalCompletionBlock)completedBlock; +/** + 设置url图片 + + @param url 图片url + @param completedBlock XHExternalCompletionBlock + */ - (void)xh_setImageWithURL:(nonnull NSURL *)url completed:(nullable XHExternalCompletionBlock)completedBlock; + +/** + 设置url图片 + + @param url 图片url + @param placeholder 占位图 + @param options XHLaunchAdImageOptions + @param completedBlock XHExternalCompletionBlock + */ - (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(XHLaunchAdImageOptions)options completed:(nullable XHExternalCompletionBlock)completedBlock; +/** + 设置url图片 + + @param url 图片url + @param placeholder 占位图 + @param GIFImageCycleOnce gif是否只循环播放一次 + @param options XHLaunchAdImageOptions + @param completedBlock XHExternalCompletionBlock + */ - (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder GIFImageCycleOnce:(BOOL)GIFImageCycleOnce options:(XHLaunchAdImageOptions)options completed:(nullable XHExternalCompletionBlock)completedBlock; @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdImageView+XHLaunchAdCache.m b/XHLaunchAd/XHLaunchAd/XHLaunchAdImageView+XHLaunchAdCache.m index 6f4d6ea..dcfe172 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdImageView+XHLaunchAdCache.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdImageView+XHLaunchAdCache.m @@ -11,53 +11,48 @@ #import "XHLaunchAdConst.h" @implementation XHLaunchAdImageView (XHLaunchAdCache) -- (void)xh_setImageWithURL:(nonnull NSURL *)url -{ +- (void)xh_setImageWithURL:(nonnull NSURL *)url{ [self xh_setImageWithURL:url placeholderImage:nil]; } -- (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder -{ + +- (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder{ [self xh_setImageWithURL:url placeholderImage:placeholder options:XHLaunchAdImageDefault]; } --(void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(XHLaunchAdImageOptions)options -{ + +-(void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(XHLaunchAdImageOptions)options{ [self xh_setImageWithURL:url placeholderImage:placeholder options:options completed:nil]; } + - (void)xh_setImageWithURL:(nonnull NSURL *)url completed:(nullable XHExternalCompletionBlock)completedBlock { [self xh_setImageWithURL:url placeholderImage:nil completed:completedBlock]; } -- (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable XHExternalCompletionBlock)completedBlock -{ + +- (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable XHExternalCompletionBlock)completedBlock{ [self xh_setImageWithURL:url placeholderImage:placeholder options:XHLaunchAdImageDefault completed:completedBlock]; } --(void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(XHLaunchAdImageOptions)options completed:(nullable XHExternalCompletionBlock)completedBlock -{ + +-(void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(XHLaunchAdImageOptions)options completed:(nullable XHExternalCompletionBlock)completedBlock{ [self xh_setImageWithURL:url placeholderImage:placeholder GIFImageCycleOnce:NO options:options completed:completedBlock]; } -- (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder GIFImageCycleOnce:(BOOL)GIFImageCycleOnce options:(XHLaunchAdImageOptions)options completed:(nullable XHExternalCompletionBlock)completedBlock -{ + +- (void)xh_setImageWithURL:(nonnull NSURL *)url placeholderImage:(nullable UIImage *)placeholder GIFImageCycleOnce:(BOOL)GIFImageCycleOnce options:(XHLaunchAdImageOptions)options completed:(nullable XHExternalCompletionBlock)completedBlock{ if(placeholder) self.image = placeholder; if(!url) return; XHWeakSelf [[XHLaunchAdImageManager sharedManager] loadImageWithURL:url options:options progress:nil completed:^(UIImage * _Nullable image, NSData *_Nullable imageData, NSError * _Nullable error, NSURL * _Nullable imageURL) { - if(XHISGIFTypeWithData(imageData)){ - weakSelf.image = nil; weakSelf.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData]; weakSelf.loopCompletionBlock = ^(NSUInteger loopCountRemaining) { - if(GIFImageCycleOnce) [weakSelf stopAnimating]; }; - }else{ - weakSelf.image = image; weakSelf.animatedImage = nil; } - if(completedBlock) completedBlock(image,imageData,error,imageURL); }]; } + @end diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchAdView.m b/XHLaunchAd/XHLaunchAd/XHLaunchAdView.m index 8cf85e5..4bebaef 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchAdView.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchAdView.m @@ -13,11 +13,9 @@ @interface XHLaunchAdImageView () @end @implementation XHLaunchAdImageView -- (id)init -{ +- (id)init{ self = [super init]; if (self) { - self.userInteractionEnabled = YES; self.frame = [UIScreen mainScreen].bounds; self.layer.masksToBounds = YES; @@ -26,8 +24,8 @@ - (id)init } return self; } --(void)tap:(UIGestureRecognizer *)gestureRecognizer -{ + +-(void)tap:(UIGestureRecognizer *)gestureRecognizer{ if(self.click) self.click(); } @@ -40,39 +38,34 @@ @interface XHLaunchAdVideoView () @implementation XHLaunchAdVideoView -- (instancetype)init -{ +- (instancetype)init{ self = [super init]; if (self) { - self.userInteractionEnabled = YES; self.frame = [UIScreen mainScreen].bounds; - [self addSubview:self.videoPlayer.view]; - UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; tapGesture.delegate = self; [self addGestureRecognizer:tapGesture]; } return self; } --(void)tap:(UIGestureRecognizer *)gestureRecognizer -{ + +-(void)tap:(UIGestureRecognizer *)gestureRecognizer{ if(self.click) self.click(); } -- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer -{ + +- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } --(void)setVideoScalingMode:(MPMovieScalingMode)videoScalingMode -{ + +-(void)setVideoScalingMode:(MPMovieScalingMode)videoScalingMode{ _videoScalingMode = videoScalingMode; _videoPlayer.scalingMode = videoScalingMode; } --(MPMoviePlayerController *)videoPlayer -{ - if(_videoPlayer==nil) - { + +-(MPMoviePlayerController *)videoPlayer{ + if(_videoPlayer==nil){ _videoPlayer = [[MPMoviePlayerController alloc] init]; _videoPlayer.shouldAutoplay = YES; [_videoPlayer setControlStyle:MPMovieControlStyleNone]; @@ -82,32 +75,26 @@ -(MPMoviePlayerController *)videoPlayer _videoPlayer.view.backgroundColor = [UIColor clearColor]; } return _videoPlayer; - } --(void)stopVideoPlayer{ +-(void)stopVideoPlayer{ if(_videoPlayer==nil) return; [_videoPlayer stop]; [_videoPlayer.view removeFromSuperview]; _videoPlayer = nil; - } + #pragma mark - set --(void)setFrame:(CGRect)frame -{ +-(void)setFrame:(CGRect)frame{ [super setFrame:frame]; _videoPlayer.view.frame = self.frame; } --(void)setVideoCycleOnce:(BOOL)videoCycleOnce -{ + +-(void)setVideoCycleOnce:(BOOL)videoCycleOnce{ _videoCycleOnce = videoCycleOnce; - - if(videoCycleOnce) - { + if(videoCycleOnce){ _videoPlayer.repeatMode = MPMovieRepeatModeNone; - } - else - { + }else{ _videoPlayer.repeatMode = MPMovieRepeatModeOne; } } diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchImageView.h b/XHLaunchAd/XHLaunchAd/XHLaunchImageView.h index 418a5c2..8fd95a2 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchImageView.h +++ b/XHLaunchAd/XHLaunchAd/XHLaunchImageView.h @@ -10,9 +10,7 @@ /** 启动图来源 */ typedef NS_ENUM(NSInteger,LaunchImagesSource){ - LaunchImagesSourceLaunchImage = 1,//LaunchAdImage (default) - LaunchImagesSourceLaunchScreen = 2,//LaunchScreen.storyboard }; diff --git a/XHLaunchAd/XHLaunchAd/XHLaunchImageView.m b/XHLaunchAd/XHLaunchAd/XHLaunchImageView.m index 03ad0ad..30d03f6 100644 --- a/XHLaunchAd/XHLaunchAd/XHLaunchImageView.m +++ b/XHLaunchAd/XHLaunchAd/XHLaunchImageView.m @@ -16,34 +16,29 @@ @interface XHLaunchImageView () @implementation XHLaunchImageView #pragma mark - private -- (instancetype)initWithLaunchImagesSource:(LaunchImagesSource)source -{ +- (instancetype)initWithLaunchImagesSource:(LaunchImagesSource)source{ self = [super init]; if (self) { self.frame = [UIScreen mainScreen].bounds; self.userInteractionEnabled = YES; self.backgroundColor = [UIColor whiteColor]; - switch (source) { - case LaunchImagesSourceLaunchImage: - { + case LaunchImagesSourceLaunchImage:{ self.image = [self imageFromLaunchImage]; } break; - case LaunchImagesSourceLaunchScreen: - { + case LaunchImagesSourceLaunchScreen:{ self.image = [self imageFromLaunchScreen]; } break; - default: break; } } return self; } --(UIImage *)imageFromLaunchImage -{ + +-(UIImage *)imageFromLaunchImage{ UIImage *imageP = [self launchImageWithType:@"Portrait"]; if(imageP) return imageP; UIImage *imageL = [self launchImageWithType:@"Landscape"]; @@ -51,26 +46,24 @@ -(UIImage *)imageFromLaunchImage XHLaunchAdLog(@"获取LaunchImage失败!请检查是否添加启动图,或者规格是否有误."); return nil; } --(UIImage *)imageFromLaunchScreen -{ + +-(UIImage *)imageFromLaunchScreen{ NSString *UILaunchStoryboardName = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchStoryboardName"]; if(UILaunchStoryboardName == nil){ XHLaunchAdLog(@"从 LaunchScreen 中获取启动图失败!"); return nil; } UIViewController *LaunchScreenSb = [[UIStoryboard storyboardWithName:UILaunchStoryboardName bundle:nil] instantiateInitialViewController]; - if(LaunchScreenSb) - { + if(LaunchScreenSb){ UIView * view = LaunchScreenSb.view; view.frame = [UIScreen mainScreen].bounds; - UIImage *image = [self imageFromView:view]; return image; } - XHLaunchAdLog(@"从 LaunchScreen 中获取启动图失败!"); return nil; } + -(UIImage*)imageFromView:(UIView*)view{ CGSize size = view.bounds.size; //参数1:表示区域大小 参数2:如果需要显示半透明效果,需要传NO,否则传YES 参数3:屏幕密度 @@ -80,24 +73,19 @@ -(UIImage*)imageFromView:(UIView*)view{ UIGraphicsEndImageContext(); return image; } --(UIImage *)launchImageWithType:(NSString *)type -{ + +-(UIImage *)launchImageWithType:(NSString *)type{ CGSize viewSize = [UIScreen mainScreen].bounds.size; NSString *viewOrientation = type; NSString *launchImageName = nil; NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"]; for (NSDictionary* dict in imagesDict){ - CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]); - if([viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]){ - if([dict[@"UILaunchImageOrientation"] isEqualToString:@"Landscape"]){ - imageSize = CGSizeMake(imageSize.height, imageSize.width); } if(CGSizeEqualToSize(imageSize, viewSize)){ - launchImageName = dict[@"UILaunchImageName"]; UIImage *image = [UIImage imageNamed:launchImageName]; return image; diff --git a/XHLaunchAdExample/XHLaunchAdManager.m b/XHLaunchAdExample/XHLaunchAdManager.m index 46beec0..24cd2ed 100644 --- a/XHLaunchAdExample/XHLaunchAdManager.m +++ b/XHLaunchAdExample/XHLaunchAdManager.m @@ -37,8 +37,7 @@ @interface XHLaunchAdManager() @implementation XHLaunchAdManager -+(void)load -{ ++(void)load{ [self shareManager]; } @@ -46,29 +45,25 @@ +(XHLaunchAdManager *)shareManager{ static XHLaunchAdManager *instance = nil; static dispatch_once_t oneToken; dispatch_once(&oneToken,^{ - instance = [[XHLaunchAdManager alloc] init]; }); return instance; } -- (instancetype)init -{ + +- (instancetype)init{ self = [super init]; if (self) { - - //在UIApplicationDidFinishLaunching时初始化开屏广告,做到对业务层无干扰 - //当然你也可以直接在AppDelegate didFinishLaunchingWithOptions方法中初始化 + //在UIApplicationDidFinishLaunching时初始化开屏广告,做到对业务层无干扰,当然你也可以直接在AppDelegate didFinishLaunchingWithOptions方法中初始化 [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { - //初始化开屏广告 [self setupXHLaunchAd]; - }]; } return self; } --(void)setupXHLaunchAd -{ + +-(void)setupXHLaunchAd{ + //1.******图片开屏广告 - 网络数据****** //[self example01]; @@ -99,8 +94,8 @@ -(void)setupXHLaunchAd #pragma mark - 图片开屏广告-网络数据-示例 //图片开屏广告 - 网络数据 --(void)example01 -{ +-(void)example01{ + //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage) [XHLaunchAd setLaunchImagesSource:LaunchImagesSourceLaunchImage]; @@ -144,8 +139,7 @@ -(void)example01 imageAdconfiguration.showEnterForeground = NO; //图片已缓存 - 显示一个 "已预载" 视图 (可选) - if([XHLaunchAd checkImageInCacheWithURL:[NSURL URLWithString:model.content]]) - { + if([XHLaunchAd checkImageInCacheWithURL:[NSURL URLWithString:model.content]]){ //设置要添加的自定义视图(可选) imageAdconfiguration.subViews = [self launchAdSubViews_alreadyView]; @@ -157,10 +151,11 @@ -(void)example01 }]; } + #pragma mark - 图片开屏广告-本地数据-示例 //图片开屏广告 - 本地数据 --(void)example02 -{ +-(void)example02{ + //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage) [XHLaunchAd setLaunchImagesSource:LaunchImagesSourceLaunchImage]; @@ -195,8 +190,8 @@ -(void)example02 #pragma mark - 视频开屏广告-网络数据-示例 //视频开屏广告 - 网络数据 --(void)example03 -{ +-(void)example03{ + //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage) [XHLaunchAd setLaunchImagesSource:LaunchImagesSourceLaunchImage]; @@ -238,8 +233,7 @@ -(void)example03 //跳过按钮类型 videoAdconfiguration.skipButtonType = SkipTypeTimeText; //视频已缓存 - 显示一个 "已预载" 视图 (可选) - if([XHLaunchAd checkVideoInCacheWithURL:[NSURL URLWithString:model.content]]) - { + if([XHLaunchAd checkVideoInCacheWithURL:[NSURL URLWithString:model.content]]){ //设置要添加的自定义视图(可选) videoAdconfiguration.subViews = [self launchAdSubViews_alreadyView]; @@ -252,10 +246,11 @@ -(void)example03 }]; } + #pragma mark - 视频开屏广告-本地数据-示例 //视频开屏广告 - 本地数据 --(void)example04 -{ +-(void)example04{ + //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage) [XHLaunchAd setLaunchImagesSource:LaunchImagesSourceLaunchImage]; @@ -288,8 +283,8 @@ -(void)example04 } #pragma mark - 自定义跳过按钮-示例 --(void)example05 -{ +-(void)example05{ + //注意: //1.自定义跳过按钮很简单,configuration有一个customSkipView属性. //2.自定义一个跳过的view 赋值给configuration.customSkipView属性便可替换默认跳过按钮,如下: @@ -330,12 +325,13 @@ -(void)example05 } + #pragma mark - 使用默认配置快速初始化 - 示例 /** * 图片 */ --(void)example06 -{ +-(void)example06{ + //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage) [XHLaunchAd setLaunchImagesSource:LaunchImagesSourceLaunchImage]; @@ -347,11 +343,12 @@ -(void)example06 imageAdconfiguration.openURLString = @"http://www.it7090.com"; [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self]; } + /** * 视频 */ --(void)example07 -{ +-(void)example07{ + //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage) [XHLaunchAd setLaunchImagesSource:LaunchImagesSourceLaunchImage]; @@ -368,8 +365,8 @@ -(void)example07 /** * 批量下载并缓存图片 */ --(void)batchDownloadImageAndCache -{ +-(void)batchDownloadImageAndCache{ + [XHLaunchAd downLoadImageAndCacheWithURLArray:@[[NSURL URLWithString:imageURL1],[NSURL URLWithString:imageURL2],[NSURL URLWithString:imageURL3],[NSURL URLWithString:imageURL4],[NSURL URLWithString:imageURL5]] completed:^(NSArray * _Nonnull completedArray) { /** 打印批量下载缓存结果 */ @@ -383,8 +380,8 @@ -(void)batchDownloadImageAndCache /** * 批量下载并缓存视频 */ --(void)batchDownloadVideoAndCache -{ +-(void)batchDownloadVideoAndCache{ + [XHLaunchAd downLoadVideoAndCacheWithURLArray:@[[NSURL URLWithString:videoURL1],[NSURL URLWithString:videoURL2],[NSURL URLWithString:videoURL3]] completed:^(NSArray * _Nonnull completedArray) { /** 打印批量下载缓存结果 */ @@ -396,9 +393,10 @@ -(void)batchDownloadVideoAndCache }]; } + #pragma mark - subViews --(NSArray *)launchAdSubViews_alreadyView -{ +-(NSArray *)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 = @"已预载"; @@ -411,8 +409,9 @@ -(void)batchDownloadVideoAndCache return [NSArray arrayWithObject:label]; } --(NSArray *)launchAdSubViews -{ + +-(NSArray *)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"; @@ -425,10 +424,11 @@ -(void)batchDownloadVideoAndCache return [NSArray arrayWithObject:label]; } + #pragma mark - customSkipView //自定义跳过按钮 --(UIView *)customSkipView -{ +-(UIView *)customSkipView{ + UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor =[UIColor orangeColor]; button.layer.cornerRadius = 5.0; @@ -441,12 +441,14 @@ -(UIView *)customSkipView [button addTarget:self action:@selector(skipAction) forControlEvents:UIControlEventTouchUpInside]; return button; } + //跳过按钮点击事件 --(void)skipAction -{ +-(void)skipAction{ + //移除广告 [XHLaunchAd removeAndAnimated:YES]; } + #pragma mark - XHLaunchAd delegate - 倒计时回调 /** * 倒计时回调 @@ -454,20 +456,20 @@ -(void)skipAction * @param launchAd XHLaunchAd * @param duration 倒计时时间 */ --(void)xhLaunchAd:(XHLaunchAd *)launchAd customSkipView:(UIView *)customSkipView duration:(NSInteger)duration -{ +-(void)xhLaunchAd:(XHLaunchAd *)launchAd customSkipView:(UIView *)customSkipView duration:(NSInteger)duration{ //设置自定义跳过按钮时间 UIButton *button = (UIButton *)customSkipView;//此处转换为你之前的类型 //设置时间 [button setTitle:[NSString stringWithFormat:@"自定义%lds",duration] forState:UIControlStateNormal]; } + #pragma mark - XHLaunchAd delegate - 其他 /** * 广告点击事件 回调 */ -- (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenURLString:(NSString *)openURLString; -{ +- (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenURLString:(NSString *)openURLString{ + NSLog(@"广告点击"); WebViewController *VC = [[WebViewController alloc] init]; VC.URLString = openURLString; @@ -475,6 +477,7 @@ - (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenURLString:(NSString *)open UIViewController* rootVC = [[UIApplication sharedApplication].delegate window].rootViewController; [rootVC.myNavigationController pushViewController:VC animated:YES]; } + /** * 图片本地读取/或下载完成回调 * @@ -482,34 +485,34 @@ - (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenURLString:(NSString *)open * @param image 读取/下载的image * @param imageData 读取/下载的imageData */ --(void)xhLaunchAd:(XHLaunchAd *)launchAd imageDownLoadFinish:(UIImage *)image imageData:(nonnull NSData *)imageData -{ +-(void)xhLaunchAd:(XHLaunchAd *)launchAd imageDownLoadFinish:(UIImage *)image imageData:(nonnull NSData *)imageData{ NSLog(@"图片下载完成/或本地图片读取完成回调"); } + /** * 视频本地读取/或下载完成回调 * * @param launchAd XHLaunchAd * @param pathURL 视频保存在本地的path */ --(void)xhLaunchAd:(XHLaunchAd *)launchAd videoDownLoadFinish:(NSURL *)pathURL -{ +-(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 -{ +-(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 -{ +-(void)xhLaunchAdShowFinish:(XHLaunchAd *)launchAd{ + NSLog(@"广告显示完成"); }