广告页经常用得到,所以自己写一个备用。因为之前也没写过所以借鉴了《iOS-APP启动页加载广告》的思路。我自己在她的基础上改了一些东西,废话少说,先上效果图:
大概算思路吧:1、封装一个ADView类(一个广告页类)一张图片、一个倒计时按钮。负责广告页的展示。
2、封装一个ADImageHandle,广告处理类,负责处理广告图片的更新、下载、保存、删除旧图。
程序流程:第一次程序启动时的广告显示没能实现,这边相当于显示的是已经保存下来的图片,如果有更新就下载保存新的图片删除旧的图片,下一次打开还是显示保存的图片。
核心代码:
1. ADView页面的封装
1 // 2 // ADView.h 3 // Demo 4 // 5 6 7 #import <UIKit/UIKit.h> 8 9 @protocol LFAdViewDelegate <NSObject> 10 - (void) pushAd; 11 @end 12 13 @interface ADView : UIView 14 @property (nonatomic, weak) id<LFAdViewDelegate> delegate; //用来实现点击跳转广告页 15 @property (nonatomic, copy) NSString *adImagePath; //广告图片存储的路径 16 17 /** 18 显示广告页 19 */ 20 - (void) show; 21 @end
1 //页面实现 2 @interface ADView () 3 @property (nonatomic, strong) UIImageView *adImageView; 4 @property (nonatomic, strong) UIButton *pushBtn; //跳过button 5 @property (nonatomic, strong) NSTimer *timer; //定时器倒计时 6 @end 7 8 //倒计时5s 9 static NSUInteger AdCount = 5; 10 11 @implementation ADView 12 13 14 - (instancetype)initWithFrame:(CGRect)frame{ 15 if (self = [super initWithFrame:frame]) { 16 [self configuareWithFrame:frame]; 17 } 18 return self; 19 } 20 21 - (void) configuareWithFrame:(CGRect)frame { 22 _adImageView = [[UIImageView alloc] initWithFrame:frame]; 23 _adImageView.userInteractionEnabled = YES; 24 // _adImageView.image = [UIImage imageNamed:@"2222.jpg"]; 25 _adImageView.contentMode = UIViewContentModeScaleAspectFill; 26 _adImageView.clipsToBounds = YES; 27 [self addSubview:_adImageView]; 28 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(linkAD)]; 29 [_adImageView addGestureRecognizer:tap]; 30 31 _pushBtn = [[UIButton alloc] init]; 32 [_pushBtn setTitle:[NSString stringWithFormat:@"跳过 %lus",(unsigned long)AdCount]forState:UIControlStateNormal]; 33 _pushBtn.titleLabel.font = [UIFont systemFontOfSize:15]; 34 _pushBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; 35 [_pushBtn setContentEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 10)]; 36 _pushBtn.layer.cornerRadius = 4.0; 37 [_pushBtn addTarget:self action:@selector(removeAdView) forControlEvents:UIControlEventTouchUpInside]; 38 [self addSubview:_pushBtn]; 39 [_pushBtn mas_makeConstraints:^(MASConstraintMaker *make) { 40 make.right.equalTo(@(-50)); 41 make.top.equalTo(@(75)); 42 }]; 43 44 }
1 //定时器的实现 2 - (void)startTimer 3 { 4 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) { 5 [self ticktock]; 6 }]; 7 } 8 9 - (void)ticktock 10 { 11 if(!AdCount){//计时器为0 12 [self removeAdView]; 13 return; 14 } 15 [_pushBtn setTitle:[NSString stringWithFormat:@"跳过 %@s",@(AdCount--)] forState:UIControlStateNormal]; 16 [_pushBtn sizeToFit]; 17 }
1 //广告页的显示 2 - (void) show 3 { 4 //启动计时器 5 [self startTimer]; 6 //展示页面 7 UIWindow *window = [UIApplication sharedApplication].keyWindow; 8 [window addSubview:self]; 9 }
2. ADImageHandle类,只暴露出一个+ (void)setupWithVC:(id)vc的方法,操作全部内部实现。
1 #import <Foundation/Foundation.h> 2 3 @interface ADImageHandle : NSObject 4 5 /** 6 设置广告页 7 8 @param vc 跳广告链接我是在ADView上写了一个协议,然后给VC去代理实现的,所以这边要传个VC, 9 如果不想传也可以用个通知的方式实现,在点击广告的时候发个通知的,在你的第一个VC里面接收通知然后跳转广告页。 10 */ 11 + (void)setupWithVC:(id)vc; 12 13 @end
1 + (void) setupWithVC:(id)vc 2 { 3 ![self imageIsExsist]?:[self ADViewShowWithImagePath:[[self getImageFilePath]stringByAppendingString:SMUserDefaultGet(kADImageName)]setDelegateVC:vc]; 4 5 [self getADImageData]; 6 }
1 /** 2 广告接口,一般来说,这里应该是访问广告类的接口,返回的应该会有一个imageURL和一个广告跳转的ADURL, 3 在这里对这两个进行处理,再根据imageURL把图片名字截取出来,跟保存的imageName进行对比,一样的就不操作,不一样就开始下载图片, 4 下载完成的时候更新imageName的值,并删除旧照片。 5 Attention:第一次我是没有显示广告的 6 */ 7 + (void)getADImageData 8 { 9 NSArray *imageList = @[ 10 @"http://images.cnblogs.com/cnblogs_com/fenglee594/1173372/o_WechatIMG189.jpg", 11 @"http://images.cnblogs.com/cnblogs_com/fenglee594/1173372/o_2222.jpg", 12 @"http://images.cnblogs.com/cnblogs_com/fenglee594/1173372/o_WechatIMG1.jpg", 13 @"http://images.cnblogs.com/cnblogs_com/fenglee594/1173372/o_WechatIMG2.jpg", 14 ]; 15 NSString *imageUrl = imageList[arc4random() % imageList.count]; 16 NSString *imageName = [[imageUrl componentsSeparatedByString:@"/"] lastObject]; 17 NSString * exsistImageName = SMUserDefaultGet(kADImageName); 18 19 //第一次获取的exsistImageName是为nil的,这时候直接是下载 20 [imageName isEqualToString:exsistImageName]?:[self downloadImageWithUrl:imageUrl ImageName:imageName DeleteOldImage:exsistImageName]; 21 }
1 /** 2 异步下载广告图片 3 4 @param imageUrl 图片URL 5 @param imageName 图片保存的名字 6 @param oldImage 旧图片的名字 7 */ 8 + (void) downloadImageWithUrl:(NSString *)imageUrl 9 ImageName:(NSString *)imageName 10 DeleteOldImage:(NSString *)oldImage 11 { 12 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 13 NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]; 14 UIImage *image = [UIImage imageWithData:imageData]; 15 NSString *filePath = [[self getImageFilePath] stringByAppendingString:imageName]; 16 if ([UIImageJPEGRepresentation(image, 1) writeToFile:filePath atomically:YES]) { 17 //保存完图片就更新ImageName,并删除旧的图片 18 SMUserDefaultSet(kADImageName, imageName); 19 [self deleteOldImage:oldImage]; 20 } 21 }); 22 23 }
1 /** 2 设置广告页的显示和代理VC 3 4 @param imagePath 加载的广告图片路径 5 @param vc 代理VC 6 */ 7 + (void) ADViewShowWithImagePath:(NSString *)imagePath 8 setDelegateVC:(id )vc 9 { 10 ADView *adView = [[ADView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 11 adView.adImagePath = imagePath; 12 adView.delegate = vc; 13 [adView show]; 14 }
1 /** 2 判断图片是否存在 3 4 @return yes 存在 no 不存在 5 */ 6 + (BOOL) imageIsExsist 7 { 8 if (!SMUserDefaultGet(kADImageName)) { 9 return NO; 10 } 11 BOOL isDir = NO; 12 if ([kFileManager fileExistsAtPath:[[self getImageFilePath] stringByAppendingString:SMUserDefaultGet(kADImageName)] isDirectory:&isDir]){ 13 return YES; 14 } else { 15 return NO; 16 } 17 }
3. 在AppDelegate中的使用
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; 4 ViewController *vc = [[ViewController alloc] init]; 5 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc]; 6 self.window.rootViewController = nc; 7 [self.window makeKeyAndVisible]; 8 9 //设置广告页 10 [ADImageHandle setupWithVC:vc]; 11 12 return YES; 13 }
主要代码就是以上这些了,感觉有很多表述不清啊,不喜勿喷,欢迎讨论。
最后附上Demo地址