Keine Beschreibung

KXLaunchAdvertisement.m 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // KXLaunchAdvertisement.m
  3. // CAISHEN
  4. //
  5. // Created by kuxuan on 2017/8/17.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXLaunchAdvertisement.h"
  9. #import "KXLaunchImageView.h"
  10. #import "KXLaunchAdImageView.h"
  11. #import "KXLaunchAdSkipButton.h"
  12. @interface KXLaunchAdvertisement ()<KXLaunchAdImageViewDelegate>
  13. @property (nonatomic, strong) KXLaunchAdConfiguration *configuration; // 启动广告配置
  14. @property (nonatomic, strong) UIWindow *window; // 启动广告窗口
  15. @property (nonatomic, strong) KXLaunchImageView *launchImageView; // 启动页视图
  16. @property (nonatomic, strong) KXLaunchAdImageView *launchAdImageView; // 启动广告视图
  17. @property (nonatomic, strong) KXLaunchAdSkipButton *skipButton; // 跳过按钮
  18. @property (nonatomic, strong) UIImageView *iconImageView; //应用icon
  19. @property (nonatomic, strong) UILabel *titleLabel; //应用名
  20. @property (nonatomic, strong) dispatch_source_t waitingTimer; // 等待时间计时器
  21. @property (nonatomic, strong) dispatch_source_t durationTimer; // 展示时间
  22. @end
  23. @implementation KXLaunchAdvertisement
  24. #pragma mark - Public
  25. + (instancetype)sharedLaunchAdvertisement {
  26. static KXLaunchAdvertisement *instance;
  27. static dispatch_once_t onceToken;
  28. dispatch_once(&onceToken, ^{
  29. instance = [[KXLaunchAdvertisement alloc] init];
  30. });
  31. return instance;
  32. }
  33. - (void)showImageLaunchAdvertisementWithConfiguration:(KXLaunchAdConfiguration *)configuration delegate:(id<KXLaunchAdvertisementDelegate>)delegate {
  34. self.configuration = configuration;
  35. _delegate = delegate;
  36. }
  37. #pragma mark - CustomAccessors
  38. - (instancetype)init {
  39. if (self = [super init]) {
  40. [self setupLaunchAdvertisement];
  41. KXLaunchModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePath:KXADVERTISEMENTID_NEW]];
  42. if (model) {
  43. [NSKeyedArchiver archiveRootObject:model toFile:[self filePath:KXADVERTISEMENTID_OLD]];
  44. }
  45. }
  46. return self;
  47. }
  48. - (void)setConfiguration:(KXLaunchAdConfiguration *)configuration {
  49. _configuration = configuration;
  50. if (configuration.imageURLString.length > 0) {
  51. [self loadLaunchAdvertisementImage];
  52. } else {
  53. [self reset];
  54. }
  55. }
  56. #pragma mark - Private
  57. /**
  58. 设置开平广告
  59. */
  60. - (void)setupLaunchAdvertisement {
  61. _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  62. _window.rootViewController = [UIViewController new];
  63. _window.rootViewController.view.backgroundColor = [UIColor clearColor];
  64. _window.rootViewController.view.userInteractionEnabled = NO;
  65. _window.windowLevel = UIWindowLevelAlert-1;
  66. _window.hidden = NO;
  67. _launchImageView = [KXLaunchImageView new];
  68. [_window addSubview:_launchImageView];
  69. [self startWaitingTimerCountdown];
  70. }
  71. /**
  72. 等待时间,如果没有请求到就跳到首页
  73. */
  74. - (void)startWaitingTimerCountdown {
  75. __block NSUInteger duration = 3;
  76. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  77. _waitingTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  78. dispatch_source_set_timer(_waitingTimer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
  79. dispatch_source_set_event_handler(_waitingTimer, ^{
  80. dispatch_async(dispatch_get_main_queue(), ^{
  81. if(duration == 0) {
  82. NSData *imageData = [[NSUserDefaults standardUserDefaults]valueForKey:KXADVERTISEMENTKEY];
  83. if (imageData) {
  84. if (_waitingTimer) {
  85. dispatch_source_cancel(_waitingTimer);
  86. _waitingTimer = nil;
  87. [self requestLocalImageData:imageData];
  88. }
  89. }else{
  90. [self reset];
  91. }
  92. }
  93. duration--;
  94. });
  95. });
  96. dispatch_resume(_waitingTimer);
  97. }
  98. /**
  99. 加载本地的图片资源
  100. */
  101. - (void)requestLocalImageData:(NSData *)imageData{
  102. CGRect launchAdvertisementImageViewFrame = CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT - 100);
  103. _launchAdImageView = [KXLaunchAdImageView new];
  104. _launchAdImageView.frame = launchAdvertisementImageViewFrame;
  105. _launchAdImageView.image = [UIImage imageWithData:imageData];
  106. _launchAdImageView.delegate = self;
  107. [_window addSubview:_launchAdImageView];
  108. [self setupSkipButton];
  109. [self setupIconName];
  110. [self startSkipDurationCountdown];
  111. }
  112. /**
  113. 重置开屏广告
  114. */
  115. - (void)reset {
  116. if (_waitingTimer) {
  117. dispatch_source_cancel(_waitingTimer);
  118. _waitingTimer = nil;
  119. }
  120. if (_durationTimer) {
  121. dispatch_source_cancel(_durationTimer);
  122. _durationTimer = nil;
  123. }
  124. [UIView animateWithDuration:0.25 animations:^{
  125. _window.alpha = 0;
  126. } completion:^(BOOL finished) {
  127. [_window.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  128. [obj removeFromSuperview];
  129. }];
  130. _window.hidden = YES;
  131. _launchImageView = nil;
  132. _launchAdImageView = nil;
  133. _skipButton = nil;
  134. _iconImageView = nil;
  135. _titleLabel = nil;
  136. _window = nil;
  137. }];
  138. }
  139. /**
  140. 加载启动视图广告
  141. */
  142. - (void)loadLaunchAdvertisementImage {
  143. if (!(_configuration.imageURLString.length > 0)) {
  144. return;
  145. }
  146. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:_configuration.imageURLString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
  147. } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
  148. if (error || !image) {
  149. return;
  150. }
  151. [[NSUserDefaults standardUserDefaults]setValue:data forKey:KXADVERTISEMENTKEY];
  152. KXLaunchModel *model = [[KXLaunchModel alloc]init];
  153. model.image_url = self.configuration.imageURLString;
  154. model.link = self.configuration.detailString;
  155. model.product_id = self.configuration.product_id;
  156. model.skip_type = self.configuration.skipType;
  157. [NSKeyedArchiver archiveRootObject:model toFile:[self filePath:KXADVERTISEMENTID_NEW]];
  158. if (!_launchAdImageView) {
  159. [self requestLocalImageData:data];
  160. [NSKeyedArchiver archiveRootObject:model toFile:[self filePath:KXADVERTISEMENTID_OLD]];
  161. }
  162. }];
  163. }
  164. /**
  165. 设置启动视图
  166. */
  167. - (void)setupSkipButton {
  168. _skipButton = [KXLaunchAdSkipButton new];
  169. _skipButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
  170. float x = SCREEN_WIDTH - 15 - 76;
  171. float y = 6+StatusBarHeight;
  172. _skipButton.frame = CGRectMake(x, y, 76, 24);
  173. _skipButton.layer.cornerRadius = 12;
  174. [_skipButton addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchUpInside];
  175. [_window addSubview:_skipButton];
  176. }
  177. - (void)setupIconName
  178. {
  179. UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)];
  180. whiteView.backgroundColor = [UIColor whiteColor];
  181. [_window addSubview:whiteView];
  182. _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0 - 23*SCREEN_MUTI, SCREEN_HEIGHT - 80, 46, 46)];
  183. _iconImageView.layer.cornerRadius = 8;
  184. _iconImageView.layer.masksToBounds = YES;
  185. NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
  186. //获取app中所有icon名字数组
  187. NSArray *iconsArr = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
  188. //取最后一个icon的名字
  189. NSString *iconLastName = [iconsArr lastObject];
  190. _iconImageView.image = [UIImage imageNamed:iconLastName];
  191. [_window addSubview:_iconImageView];
  192. _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-100+30+46, SCREEN_WIDTH, 16)];
  193. _titleLabel.text = [infoDict objectForKey:@"CFBundleDisplayName"];
  194. _titleLabel.font = FONT_SYS(14);
  195. _titleLabel.textAlignment = NSTextAlignmentCenter;
  196. _titleLabel.textColor = [UIColor KXColorWithHex:0x666666];
  197. [_window addSubview:_titleLabel];
  198. }
  199. /**
  200. 开始倒计时
  201. */
  202. - (void)startSkipDurationCountdown {
  203. if(_waitingTimer) {
  204. dispatch_source_cancel(_waitingTimer);
  205. _waitingTimer = nil;
  206. }
  207. __block NSInteger duration = 3;
  208. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  209. _durationTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  210. dispatch_source_set_timer(_durationTimer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
  211. dispatch_source_set_event_handler(_durationTimer, ^{
  212. dispatch_async(dispatch_get_main_queue(), ^{
  213. _skipButton.duration = duration;
  214. if(duration == 0) {
  215. if (_waitingTimer) {
  216. dispatch_source_cancel(_durationTimer);
  217. _durationTimer = nil;
  218. }
  219. [self reset];
  220. }
  221. duration--;
  222. });
  223. });
  224. dispatch_resume(_durationTimer);
  225. }
  226. #pragma mark - KXLaunchAdImageViewDelegate
  227. - (void)didSelectAdvertisementImageView:(KXLaunchAdImageView *)adImageView
  228. {
  229. KXLaunchModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePath:KXADVERTISEMENTID_OLD]];
  230. if ([_delegate respondsToSelector:@selector(launchAdvertisement:didSelectedWithLaunchModel:)]) {
  231. [_delegate launchAdvertisement:self didSelectedWithLaunchModel:model];
  232. [self reset];
  233. }
  234. }
  235. - (NSString *)filePath:(NSString *)path {
  236. NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  237. return [cachesPath stringByAppendingPathComponent:path];
  238. }
  239. @end