使用场景:
有不少App启动时会加载启动页和闪屏广告,对闪屏广告时间控制一般有好几种方案
这里讲一下我在实际开发中最常用的一种解决方案,大体思路是应用启动时先进入闪屏加载流程
在获取广告图时将expire图片失效时间加到header头中来控制广告的过期时间。将首次加载广告
的ctime和expire及广告的key存储到本地sqlite3数据库中,这样只要在有效期expire内就显示本地之
前加载的广告就可以了。
首次加载广告主要代码如下:
/** * 系统启动时调用 * * @param application * @param launchOptions */ - (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _lock = [[NSRecursiveLock alloc] init]; //启动广告图 _splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, FD_SCREEN_MAIN_HEIGHT-114)]; _splashImageView.alpha = 0; _splashImageView.contentMode = UIViewContentModeScaleAspectFill; UIView *_botIconView = [[UIView alloc] initWithFrame:CGRectMake(0, FD_SCREEN_MAIN_HEIGHT-114, FD_SCREEN_MAIN_WIDTH, 114)]; _botIconView.backgroundColor = [UIColor whiteColor]; //底部logo UIImageView *_iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (114-77)/2+20, FD_SCREEN_MAIN_WIDTH, 77)]; _iconImgView.image = [UIImage imageNamed:@"bg_splash_screen"]; _iconImgView.backgroundColor = [UIColor whiteColor]; [_botIconView addSubview:_iconImgView]; [_splashImageView addSubview:_botIconView]; _advDic = [self isLocalExpires]; //当前时间 NSTimeInterval _currentTime = [[NSDate date] timeIntervalSince1970]; if (([_advDic count] && ((_currentTime-[[_advDic objectForKey:@"ctime"] intValue]) <= ADV_LIFE_EXPIRE) && ((_currentTime-[[_advDic objectForKey:@"ctime"] intValue]) < [[_advDic objectForKey:@"expire"] intValue]))) {//已经缓存过 //将启动广告加入主视图window中 [self addWindow:_splashImageView]; UIImage *_image = [UIImage readWithFile:[NSString stringWithFormat:@"%@%@", SPLASH_SAVE_VALUE, @".png"]]; [_lock lock]; //动画滑出 [UIView animateWithDuration:.3f animations:^{ _splashImageView.alpha = 1; _splashImageView.image = _image; }]; [timer invalidate]; timer = nil; timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(enterMainLogic) userInfo:nil repeats:NO]; [_lock unlock]; }else{ //获取远程图片 //判断是否wifi if ([SystemUtility IsEnableWIFI]) { [self downImageWithSplash]; }else{ [self enterMainLogic]; } } }
/** * 将启动广告加入主视图window中 */ - (void)addWindow:(UIImageView *)aImageView { UIWindow *window = ((AppDelegate*)[UIApplication sharedApplication].delegate).window; window.backgroundColor = [UIColor whiteColor]; UIViewController *rvc = [[UIViewController alloc] init]; rvc.view.frame = CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, FD_SCREEN_MAIN_HEIGHT-114); [rvc.view addSubview:aImageView]; window.rootViewController = rvc; [window makeKeyAndVisible]; } /** * 读取本地图片 * * @return */ - (NSDictionary *)isLocalExpires { return [[DbAdv singleton] readAdvByKey:SPLASH_SAVE_VALUE]; } /** * 从服务器加载下载图片 */ -(void) downImageWithSplash { NSString *_imgUrl = [NSString stringWithFormat:@"%@/get", [[NSObject DTGetConfig] objectForKey:@"apiUrl"]]; NSURL *url = [NSURL URLWithString:_imgUrl]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; AFHTTPRequestOperation *afOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; afOperation.inputStream = [NSInputStream inputStreamWithURL:url]; NSArray *pathAry = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); //并给文件起个文件名 NSString *fileName = [NSString stringWithFormat:@"%@%@", SPLASH_SAVE_VALUE, @".png"]; NSString *filePath = [[pathAry objectAtIndex:0] stringByAppendingPathComponent:fileName]; afOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO]; //已完成下载 [afOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { //保存进sqlite(测试用) NSString *expire = [operation.response.allHeaderFields objectForKey:@"expire"]; if(([expire intValue]>0)){ //显示闪屏广告 _splashImageView.image = [UIImage imageWithContentsOfFile:fileName]; [_splashImageView setNeedsDisplay]; //更新到数据库 [[DbAdv singleton] updateToDB:SPLASH_SAVE_VALUE expire:expire]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; [afOperation start]; //进入app主流程 [self enterMainLogic]; }