// // KXLaunchAdvertisement.m // CAISHEN // // Created by kuxuan on 2017/8/17. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXLaunchAdvertisement.h" #import "KXLaunchImageView.h" #import "KXLaunchAdImageView.h" #import "KXLaunchAdSkipButton.h" @interface KXLaunchAdvertisement () @property (nonatomic, strong) KXLaunchAdConfiguration *configuration; // 启动广告配置 @property (nonatomic, strong) UIWindow *window; // 启动广告窗口 @property (nonatomic, strong) KXLaunchImageView *launchImageView; // 启动页视图 @property (nonatomic, strong) KXLaunchAdImageView *launchAdImageView; // 启动广告视图 @property (nonatomic, strong) KXLaunchAdSkipButton *skipButton; // 跳过按钮 @property (nonatomic, strong) UIImageView *iconImageView; //应用icon @property (nonatomic, strong) UILabel *titleLabel; //应用名 @property (nonatomic, strong) dispatch_source_t waitingTimer; // 等待时间计时器 @property (nonatomic, strong) dispatch_source_t durationTimer; // 展示时间 @end @implementation KXLaunchAdvertisement #pragma mark - Public + (instancetype)sharedLaunchAdvertisement { static KXLaunchAdvertisement *instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[KXLaunchAdvertisement alloc] init]; }); return instance; } - (void)showImageLaunchAdvertisementWithConfiguration:(KXLaunchAdConfiguration *)configuration delegate:(id)delegate { self.configuration = configuration; _delegate = delegate; } #pragma mark - CustomAccessors - (instancetype)init { if (self = [super init]) { [self setupLaunchAdvertisement]; KXLaunchModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePath:KXADVERTISEMENTID_NEW]]; if (model) { [NSKeyedArchiver archiveRootObject:model toFile:[self filePath:KXADVERTISEMENTID_OLD]]; } } return self; } - (void)setConfiguration:(KXLaunchAdConfiguration *)configuration { _configuration = configuration; if (configuration.imageURLString.length > 0) { [self loadLaunchAdvertisementImage]; } else { [self reset]; } } #pragma mark - Private /** 设置开平广告 */ - (void)setupLaunchAdvertisement { _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; _window.rootViewController = [UIViewController new]; _window.rootViewController.view.backgroundColor = [UIColor clearColor]; _window.rootViewController.view.userInteractionEnabled = NO; _window.windowLevel = UIWindowLevelAlert-1; _window.hidden = NO; _launchImageView = [KXLaunchImageView new]; [_window addSubview:_launchImageView]; [self startWaitingTimerCountdown]; } /** 等待时间,如果没有请求到就跳到首页 */ - (void)startWaitingTimerCountdown { __block NSUInteger duration = 3; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); _waitingTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(_waitingTimer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0); dispatch_source_set_event_handler(_waitingTimer, ^{ dispatch_async(dispatch_get_main_queue(), ^{ if(duration == 0) { NSData *imageData = [[NSUserDefaults standardUserDefaults]valueForKey:KXADVERTISEMENTKEY]; if (imageData) { if (_waitingTimer) { dispatch_source_cancel(_waitingTimer); _waitingTimer = nil; [self requestLocalImageData:imageData]; } }else{ [self reset]; } } duration--; }); }); dispatch_resume(_waitingTimer); } /** 加载本地的图片资源 */ - (void)requestLocalImageData:(NSData *)imageData{ CGRect launchAdvertisementImageViewFrame = CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT - 100); _launchAdImageView = [KXLaunchAdImageView new]; _launchAdImageView.frame = launchAdvertisementImageViewFrame; _launchAdImageView.image = [UIImage imageWithData:imageData]; _launchAdImageView.delegate = self; [_window addSubview:_launchAdImageView]; [self setupSkipButton]; [self setupIconName]; [self startSkipDurationCountdown]; } /** 重置开屏广告 */ - (void)reset { if (_waitingTimer) { dispatch_source_cancel(_waitingTimer); _waitingTimer = nil; } if (_durationTimer) { dispatch_source_cancel(_durationTimer); _durationTimer = nil; } [UIView animateWithDuration:0.25 animations:^{ _window.alpha = 0; } completion:^(BOOL finished) { [_window.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL * _Nonnull stop) { [obj removeFromSuperview]; }]; _window.hidden = YES; _launchImageView = nil; _launchAdImageView = nil; _skipButton = nil; _iconImageView = nil; _titleLabel = nil; _window = nil; }]; } /** 加载启动视图广告 */ - (void)loadLaunchAdvertisementImage { if (!(_configuration.imageURLString.length > 0)) { return; } [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:_configuration.imageURLString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { if (error || !image) { return; } [[NSUserDefaults standardUserDefaults]setValue:data forKey:KXADVERTISEMENTKEY]; KXLaunchModel *model = [[KXLaunchModel alloc]init]; model.image_url = self.configuration.imageURLString; model.link = self.configuration.detailString; model.product_id = self.configuration.product_id; model.skip_type = self.configuration.skipType; [NSKeyedArchiver archiveRootObject:model toFile:[self filePath:KXADVERTISEMENTID_NEW]]; if (!_launchAdImageView) { [self requestLocalImageData:data]; [NSKeyedArchiver archiveRootObject:model toFile:[self filePath:KXADVERTISEMENTID_OLD]]; } }]; } /** 设置启动视图 */ - (void)setupSkipButton { _skipButton = [KXLaunchAdSkipButton new]; _skipButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4]; float x = SCREEN_WIDTH - 15 - 76; float y = 6+StatusBarHeight; _skipButton.frame = CGRectMake(x, y, 76, 24); _skipButton.layer.cornerRadius = 12; [_skipButton addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchUpInside]; [_window addSubview:_skipButton]; } - (void)setupIconName { UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)]; whiteView.backgroundColor = [UIColor whiteColor]; [_window addSubview:whiteView]; _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0 - 23*SCREEN_MUTI, SCREEN_HEIGHT - 80, 46, 46)]; _iconImageView.layer.cornerRadius = 8; _iconImageView.layer.masksToBounds = YES; NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; //获取app中所有icon名字数组 NSArray *iconsArr = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"]; //取最后一个icon的名字 NSString *iconLastName = [iconsArr lastObject]; _iconImageView.image = [UIImage imageNamed:iconLastName]; [_window addSubview:_iconImageView]; _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-100+30+46, SCREEN_WIDTH, 16)]; _titleLabel.text = [infoDict objectForKey:@"CFBundleDisplayName"]; _titleLabel.font = FONT_SYS(14); _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.textColor = [UIColor KXColorWithHex:0x666666]; [_window addSubview:_titleLabel]; } /** 开始倒计时 */ - (void)startSkipDurationCountdown { if(_waitingTimer) { dispatch_source_cancel(_waitingTimer); _waitingTimer = nil; } __block NSInteger duration = 3; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); _durationTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(_durationTimer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0); dispatch_source_set_event_handler(_durationTimer, ^{ dispatch_async(dispatch_get_main_queue(), ^{ _skipButton.duration = duration; if(duration == 0) { if (_waitingTimer) { dispatch_source_cancel(_durationTimer); _durationTimer = nil; } [self reset]; } duration--; }); }); dispatch_resume(_durationTimer); } #pragma mark - KXLaunchAdImageViewDelegate - (void)didSelectAdvertisementImageView:(KXLaunchAdImageView *)adImageView { KXLaunchModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePath:KXADVERTISEMENTID_OLD]]; if ([_delegate respondsToSelector:@selector(launchAdvertisement:didSelectedWithLaunchModel:)]) { [_delegate launchAdvertisement:self didSelectedWithLaunchModel:model]; [self reset]; } } - (NSString *)filePath:(NSString *)path { NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; return [cachesPath stringByAppendingPathComponent:path]; } @end