在启动页后显示广告页对体验来说不可取的,但是公司以利益为主这个功能就产生了。
添加广告页有两种思路:1.先下载完成后直接显示 2.先保存下次打开在显示;
先下载直接先显示
我最新想到的就是下载完成后直接显示,接口失败或者超时则以假启动页过渡显示主页。可是我写到显示广告页就停止了,因为无论怎么写都会出现广告缺失的情况,而且启动页显示时间加长这两种都对体验很不好。所以放弃的这种方式。
先保存下次打开显示
在网上发现这个朋友的思路很好,参考他的方法完成了我想要的效果,非常感谢。
1.我们的APP用户第一次打开是显示引导页不需要显示广告的,这个时候就可以下载广告相关图片、链接完成以后把广告图片保存本地,下次打开APP判断图片是否存在以此判断广告页是否显示,思路很清晰有图片就显示没图片就不显示;
2.这种方式没有启动图延迟显示的问题也不用添加假启动页的相关逻辑;
3.对于用户来说不会出现广告缺失的情况,不用考虑网络问题。
代码
1.一行代码接入广告加载
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *controller = [[ViewController alloc] init];
self.rootNav = [[UINavigationController alloc] initWithRootViewController:controller];
self.window.rootViewController = self.rootNav;
[self.window makeKeyAndVisible];
//一行代码接入广告页
[[AdvertiseManage sharedManage] advertiseViewShow];
return YES;
}
2.判断广告图片是否存在,存在就显示广告页;无论什么情况我们都要更新广告接口
- (void)advertiseViewShow {
//1.判断沙盒中是否存在广告图片,如果存在直接显示
NSString *filePath = [self getFilePathWithImageName:[AdUserDefaults valueForKey:adImageName]];
NSString *adDetails = [AdUserDefaults valueForKey:adUrlString];
BOOL isExist = [self isFileExistWithFilePath:filePath];
if (isExist) { //图片存在
AdvertiseView *advertiseView = [[AdvertiseView alloc] initWithFrame:[UIScreen mainScreen].bounds];
advertiseView.imageFilePath = filePath;
if (adDetails) {
advertiseView.isAdClick = YES;
}
[advertiseView show];
}
//2.无论沙盒中是否存在广告图片,都需要重新调用广告接口,判断广告接口是否更新
[self getAdvertiseImage];
}
异步下载图片
/**
* 下载新图片
*/
- (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];
[AdUserDefaults setValue:imageName forKey:adImageName];
[AdUserDefaults setValue:@"http://baidu.com" forKey:adUrlString];//如果有广告链接,将广告链接也保存下来
[AdUserDefaults synchronize];
}else {
NSLog(@"保存失败");
}
});
}
/**
* 删除旧广告相关
*/
- (void)deleteOldImage {
NSString *imageName = [AdUserDefaults valueForKey:adImageName];
if (imageName) {
NSString *filePath = [self getFilePathWithImageName:imageName];
NSFileManager *fileManage = [NSFileManager defaultManager];
[fileManage removeItemAtPath:filePath error:nil];
//把广告链接也要删掉
[AdUserDefaults setValue:nil forKey:adUrlString];
[AdUserDefaults synchronize];
}
}
3.添加定时器
- (void)setUpTimer {
_count = showTime;
if (_timer == nil) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeCountShow) userInfo:nil repeats:YES];
}
}
- (void)changeCountShow {
_count --;
NSLog(@"count:%ld",(long)_count);
if (_count == 0) {
[self dismiss];
}else {
[_skipButton setTitle:[NSString stringWithFormat:@"跳过 %ld",(long)_count] forState:UIControlStateNormal];
}
}
4.利用通知来push广告详情页
//发送通知主页跳转到详情页
- (void)pushToAd {
[self dismiss];
//添加通知 push广告详情页
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushToAd" object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToAd) name:@"pushToAd" object:nil];
self.title = @"首页";
self.view.backgroundColor = [UIColor redColor];
}
- (void)pushToAd {
AdvertiseViewController *next = [[AdvertiseViewController alloc] init];
next.urlString = @"http://www.baidu.com";
[self.navigationController pushViewController:next animated:YES];
}
以上就是最后的代码逻辑