概述

加载广告页, 展现跳过按钮实现倒计时功能, 并判断广告页面是否更新.

详细

代码下载:http://www.demodashi.com/demo/10698.html

目前市场上很多APP(如淘宝、美团、微博、UC)在启动图加载完毕后,还会显示几秒的广告,右上角都有个跳过按钮可以选择立即跳过这个广告,有的APP在点击广告页之后还会进入一个广告页。
他们玩的乐此不疲,产品汪们见了自然也是不会放过这个效果!

一、主要思路

  • 1. 封装广告页, 展现跳过按钮实现倒计时功能

  • 2.判断广告页面是否更新。异步下载新图片, 删除老图片

  • 3.广告页显示

  • 4.广告页点击后展示页

二、程序实现

Step1. 封装广告页, 展现跳过按钮实现倒计时功能

ZLAdvertView.h: 先封装出来广告页,露出显示广告页面方法和图片路径

/**
*  显示广告页面方法
*/
- (void)show;
/***  图片路径*/
@property (nonatomic, copy) NSString *filePath;

ZLAdvertView.m:

定义广告显示的时间:

static int const showtime = 3;
  1. 为广告页面添加一个点击手势,跳转到广告页面.

- (NSTimer *)countTimer {if (!_countTimer) {_countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];}return _countTimer;
}
- (instancetype)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {// 1.广告图片_adView = [[UIImageView alloc] initWithFrame:frame];_adView.userInteractionEnabled = YES;_adView.contentMode = UIViewContentModeScaleAspectFill;_adView.clipsToBounds = YES;// 为广告页面添加一个点击手势,跳转到广告页面UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToAd)];[_adView addGestureRecognizer:tap];// 2.跳过按钮CGFloat btnW = 60;CGFloat btnH = 30;_countBtn = [[UIButton alloc] initWithFrame:CGRectMake(kscreenWidth - btnW - 24, btnH, btnW, btnH)];[_countBtn addTarget:self action:@selector(removeAdvertView) forControlEvents:UIControlEventTouchUpInside];[_countBtn setTitle:[NSString stringWithFormat:@"跳过%d", showtime] forState:UIControlStateNormal];_countBtn.titleLabel.font = [UIFont systemFontOfSize:15];[_countBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];_countBtn.backgroundColor = [UIColor colorWithRed:38 /255.0 green:38 /255.0 blue:38 /255.0 alpha:0.6];_countBtn.layer.cornerRadius = 4;[self addSubview:_adView];[self addSubview:_countBtn];}return self;
}
- (void)setFilePath:(NSString *)filePath {_filePath = filePath;_adView.image = [UIImage imageWithContentsOfFile:filePath];
}
- (void)pushToAd {[self removeAdvertView];[[NSNotificationCenter defaultCenter] postNotificationName:@"ZLPushToAdvert" object:nil userInfo:nil];
}

2. 广告页面的跳过按钮倒计时功能可以通过定时器或者GCD实现(这里以广告倒计时3s 做例子)

- (void)countDown {_count --;[_countBtn setTitle:[NSString stringWithFormat:@"跳过%d",_count] forState:UIControlStateNormal];if (_count == 0) {[self removeAdvertView];}
}

广告页面的跳过按钮倒计时功能可以通过定时器或者GCD实现:

- (void)show {// 倒计时方法1:GCD
//    [self startCoundown];// 倒计时方法2:定时器[self startTimer];UIWindow *window = [UIApplication sharedApplication].keyWindow;[window addSubview:self];
}

定时器倒计时:

- (void)startTimer {_count = showtime;[[NSRunLoop mainRunLoop] addTimer:self.countTimer forMode:NSRunLoopCommonModes];
}

GCD倒计时:

- (void)startCoundown {__weak __typeof(self) weakSelf = self;__block int timeout = showtime + 1; //倒计时时间 + 1dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0 * NSEC_PER_SEC, 0); //每秒执行dispatch_source_set_event_handler(_timer, ^{if(timeout <= 0){ //倒计时结束,关闭dispatch_source_cancel(_timer);dispatch_async(dispatch_get_main_queue(), ^{[weakSelf removeAdvertView];});}else{dispatch_async(dispatch_get_main_queue(), ^{[_countBtn setTitle:[NSString stringWithFormat:@"跳过%d",timeout] forState:UIControlStateNormal];});timeout--;}});dispatch_resume(_timer);
}

移除广告页面:

- (void)removeAdvertView {// 停掉定时器[self.countTimer invalidate];self.countTimer = nil;[UIView animateWithDuration:0.3f animations:^{self.alpha = 0.f;} completion:^(BOOL finished) {[self removeFromSuperview];}];}

Step2. 判断广告页面是否更新。异步下载新图片, 删除老图片

因为广告页的内容要实时显示,在无网络状态或者网速缓慢的情况下不能延迟加载,或者等到首页出现了再加载广告页。所以这里不采用网络请求广告接口去获取图片地址然后加载图片的方式,而是先将图片异步下载到本地,并保存图片名,每次打开app时先根据本地存储的图片名查找沙盒中是否存在该图片,如果存在,则显示广告页。

在AppDelegate.m 里设置启动页广告:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]];[self.window makeKeyAndVisible];// 设置启动页广告[self setupAdvert];return YES;
}
  1. 判断沙盒中是否存在广告图片,如果存在,直接显示

/***  设置启动页广告*/
- (void)setupAdvert {// 1.判断沙盒中是否存在广告图片,如果存在,直接显示NSString *filePath = [self getFilePathWithImageName:[kUserDefaults valueForKey:adImageName]];BOOL isExist = [self isFileExistWithFilePath:filePath];if (isExist) { // 图片存在ZLAdvertView *advertView = [[ZLAdvertView alloc] initWithFrame:self.window.bounds];advertView.filePath = filePath;[advertView show];}// 2.无论沙盒中是否存在广告图片,都需要重新调用广告接口,判断广告是否更新[self getAdvertisingImage];
}
/***  判断文件是否存在*/
- (BOOL)isFileExistWithFilePath:(NSString *)filePath {NSFileManager *fileManager = [NSFileManager defaultManager];BOOL isDirectory = FALSE;return [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
}

2.无论沙盒中是否存在广告图片,都需要重新调用获取广告接口,判断广告是否更新

/***  初始化广告页面*/
- (void)getAdvertisingImage {// TODO 请求广告接口// 这里原本应该采用广告接口,现在用一些固定的网络图片url代替NSArray *imageArray = @[@"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",@"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",@"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"];NSString *imageUrl = imageArray[arc4random() % imageArray.count];// 获取图片名:43-130P5122Z60-50.jpgNSArray *stringArr = [imageUrl componentsSeparatedByString:@"/"];NSString *imageName = stringArr.lastObject;// 拼接沙盒路径NSString *filePath = [self getFilePathWithImageName:imageName];BOOL isExist = [self isFileExistWithFilePath:filePath];if (!isExist){ // 如果该图片不存在,则删除老图片,下载新图片[self downloadAdImageWithUrl:imageUrl imageName:imageName];}
}

3.异步下载新图片, 删除老图片

/***  下载新图片*/
- (void)downloadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName {dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];UIImage *image = [UIImage imageWithData:data];NSString *filePath = [self getFilePathWithImageName:imageName]; // 保存文件的名称if ([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]) {// 保存成功NSLog(@"保存成功");[self deleteOldImage];[kUserDefaults setValue:imageName forKey:adImageName];[kUserDefaults synchronize];// 如果有广告链接,将广告链接也保存下来}else{NSLog(@"保存失败");}});
}
/***  删除旧图片*/
- (void)deleteOldImage {NSString *imageName = [kUserDefaults valueForKey:adImageName];if (imageName) {NSString *filePath = [self getFilePathWithImageName:imageName];NSFileManager *fileManager = [NSFileManager defaultManager];[fileManager removeItemAtPath:filePath error:nil];}
}
/***  根据图片名拼接文件路径*/
- (NSString *)getFilePathWithImageName:(NSString *)imageName {if (imageName) {NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName];return filePath;}return nil;
}

Step3. 广告页显示

广告页的显示代码可以放在AppDeleate中,也可以放在首页的控制器中。如果代码是在AppDelegate中,可以通过发送通知的方式,让首页push到广告详情页.

首页控制器HomeViewController.m:

#import "HomeViewController.h"
#import "ZLAdvertViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad {[super viewDidLoad];self.title = @"首页";self.view.backgroundColor = [UIColor greenColor];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToAd) name:@"ZLPushToAdvert" object:nil];
}
// 进入广告链接页
- (void)pushToAd {ZLAdvertViewController *adVc = [[ZLAdvertViewController alloc] init];[self.navigationController pushViewController:adVc animated:YES];}
@end

Step4. 广告页点击后展示页。

如果点击广告需要跳转广告详情页面,那么广告链接地址也需要用NSUserDefaults存储。注意:广告详情页面是从首页push进去的

广告链接页ZLAdvertViewController, 露出广告url :

@property (nonatomic, copy) NSString *adUrl;

ZLAdvertViewController.m 测试连接 :

- (void)setAdUrl:(NSString *)adUrl {_adUrl = adUrl;
}

三、压缩文件截图及运行效果

1、压缩文件截图

2、 运行时的效果

启动页进入广告效果:

启动页跳过广告效果:

四、其他补充

注意:

广告页面的底部和启动图的底部一般都是相同的,给用户的错觉就是启动图加载完之后把广告图放在了启动图上,而且不能有偏差。所以我们开发要图时需要提醒美工在制作广告图的时候要注意下。

界面性问题可以根据自己项目需求调整即可, 具体可参考代码, 项目能够直接运行!

注:本文著作权归作者,由demo大师(http://www.demodashi.com)宣传,拒绝转载,转载需要作者授权

iOS-APP启动页加载广告相关推荐

  1. iOS之深入解析App启动dyld加载流程的底层原理

    dyld 简介 一.什么是dyld? dyld 是英文 the dynamic link editor 的简写,意为动态链接器,是苹果操作系统的一个重要的组成部分. 在 iOS/Mac OSX 系统中 ...

  2. App启动界面加载自己想要的图片

    1.利用系统自带的LanuchSreen,每次启动程序时,都会自动启动自带的LanuchScreen.xib文件:当然,只有在Xcode6之后才有... 2.直接看代码: 3.启动程序的画面效果: 4 ...

  3. iOS APP启动页更新失败

    如果已经更换了启动图,有些手机启动图更新成功,有些更新失败,则有可能是缓存问题, 在/Library/SplashBoard文件夹里面保存着 启动页的缓存.更新之后删除一次就好了.为啥缓存没有自动更新 ...

  4. Android 启动页加载gif、视频、图片、并下载到本地

    之前了解了一些这方面的知识,网上具体没有一个全面的,由于近期项目用到了,所以就记录一下,大神勿喷- 效果图就不奉上了,公司保密嘛- 需求描述: 当播放类型为1时,展示为显示图片 当播放类型为2时,展示 ...

  5. android启动时加载引导图片并全屏显示

    前言:最近在做一个项目,项目要求app启动时加载引导图片,由于经验不足(技术一般般),在设计时踩了几个坑,不过好在最终也是做了出来,下面我把我的想法和步骤列一下,希望可以帮到各位新手android程序 ...

  6. ios App启动加载广告页面思路

    需求 很多app(如淘宝.美团等)在启动图加载完毕后,还会显示几秒的广告,一般都有个跳过按钮可以跳过这个广告,有的app在点击广告页之后还会进入一个广告页面,点击返回进入首页.虽然说这个广告页面对用户 ...

  7. 关于App启动加载广告页面思路

    需求 很多app(如淘宝.美团等)在启动图加载完毕后,还会显示几秒的广告,一般都有个跳过按钮可以跳过这个广告,有的app在点击广告页之后还会进入一个广告页面,点击返回进入首页.虽然说这个广告页面对用户 ...

  8. app启动页数秒加载 代码_干货 | App 自动化测试痛点(弹框及首页启动加载完成判断处理)

    1. 常见痛点 App 自动化测试中有些常见痛点问题,如果框架不能很好的处理,就可能出现元素定位超时找不到的情况,自动化也就被打断终止了.很容易打消做自动化的热情,导致从入门到放弃.比如下面的两个问题 ...

  9. ios启动页尺寸_关于移动端App启动页的策划方案

    App启动页是指app在启东时需要加载必要的运行环境和配置,在这个过程中提示用户等待的一个过渡页面. 在产品经理眼里启动页是app给予用户重要的第一印象:也是App最重要的黄金页面之一,所有用户100 ...

最新文章

  1. [Delphi] Webbroker ISAPI 示例说明
  2. 如何把选择屏幕放到标准屏幕上
  3. 01背包问题从简单到复杂
  4. dos 一行两条命令
  5. 推荐算法中用户画像构建
  6. 斯诺登的密码(洛谷-P1603)
  7. SIA:全球半导体行业销售额7月达到454亿美元 创下月度纪录
  8. leetcode 387. 字符串中的第一个唯一字符(First Unique Character in a String)
  9. 为Editplus安装smali代码语法高亮插件
  10. pow计算x的y次方
  11. Mysql导入导出时遇到的问题
  12. python日历下拉框_selenium+Python(Js处理日历控件)
  13. JD_Source Code for problem 1259
  14. ajax escape用法,ie11下ajax用escape发送中文参数失败
  15. 浏览计算机已查找驱动程序软件,欢迎访问海南省数字证书认证中心
  16. 在字节跳动实习的真实感受究竟如何?
  17. 买服务器上国外网站,国外服务器怎么买?
  18. git删除分支时究竟会删掉哪些东西
  19. 令人垂涎的武汉八大名吃
  20. java毕业设计旅拍平台源码+lw文档+mybatis+系统+mysql数据库+调试

热门文章

  1. 关于精英版stm32从模板工程移植RTT Spi驱动打开后编译不过的处理办法
  2. leecode-3无重复字符串的最长子字符串C版-滑动窗口
  3. 以太网帧的最小长度_802.3?以太网?看完你就懂了
  4. 用电脑更新手机ios系统_macOS 11正式版:大更新!苹果把iOS风格搬到了电脑上
  5. php 遍历目录下的子目录文件,PHP获取目录及子目录下指定后缀的所有文件
  6. Redis和Memcache区别
  7. C语言复习4_while循环
  8. SQLServer学习笔记系列5
  9. 真实的布兰妮,有点壮
  10. oracle的分析函数over 及开窗函数[转]