之前有有些过简单的广告页的实现,现在我对之前的写的进行细致的优化使它更有效地应用到我们的项目中去。
还是说一下他的原理吧
遮盖首页显示广告页
其中:广告放在首页的最顶层视图上,这样做法的好处是点击了广告页跳转到相关html页面后,返回的页面仍是首页。
目前的话广告页支持一般图片和gif图片(如果有播放视频需求的童鞋,看到这里就可以return了)
界面内容:广告显示时间,全屏点击进入web页面。
技术点:定时器,图片下载缓存,下载超时控制等。
核心代码:
//数据加载类
-(void)loadData{
//设置默认数据
if (self.timeoutInterval == 0) {
self.timeoutInterval = 5.0;
}
if (self.dayTimes == 0) {
self.dayTimes = 99999;
};
NSString *imagePath = self.pictureUrl;
__weak typeof(self) weakSelf = self;
[self imageDataWihtUrl:imagePath callback:^(HHRADPictureType type, id data) {
if ([data isKindOfClass:[NSData class]]) {
if (type == HHRADPictureGIF) {
UIImage *image = [UIImage sd_animatedGIFWithData:data];
weakSelf.adView.adImageView.image = image;
}else{
weakSelf.adView.adImageView.image = [UIImage imageWithData:data];
}
}else if([data isKindOfClass:[NSString class]]){
[self loadDownPictureSourceWithUrl:data callback:^(HHRADPictureType type, id data) {
dispatch_async(dispatch_get_main_queue(), ^{
if (type == HHRADPictureGIF) {
UIImage *image = [UIImage sd_animatedGIFWithData:data];
weakSelf.adView.adImageView.image = image;
}else{
weakSelf.adView.adImageView.image = [UIImage imageWithData:data];
}
});
}];
}else{
//后续补充其他类型
}
}];
}
/**
开启图片下载操作
@param api 图片资源链接
@param callback 操作数据返回
*/
-(void)loadDownPictureSourceWithUrl:(NSString *)api callback:(CompletionBlock)callback{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:api] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:self.timeoutInterval];
NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request];
//开始任务
[task resume];
//完成回调
_completionBlock = callback;
}
#pragma mark --- NSURLSessionDownloadDelegate
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
//下载完成
BOOL isSeccess = [[NSFileManager defaultManager] moveItemAtPath:[location path] toPath:[self filePathWihtImagePath:self.pictureUrl] error:nil];
if (isSeccess) {
NSLog(@"广告页已缓存");
__weak typeof(self) weakSelf = self;
[self imageDataWihtUrl:self.pictureUrl callback:^(HHRADPictureType type, id data) {
weakSelf.completionBlock(type,data);
}];
}else{
NSLog(@"广告页缓存出现问题请排查");
}
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
//显示下载进度(暂不需要)
// NSLog(@"下载进度:%f",1.0*totalBytesWritten/totalBytesExpectedToWrite);
}
-(void)URLSession:(NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error{
//下载出错(退出广告界面)
if (error) {
NSLog(@"资源下载出错:%ld",[error code]);
[self.adView dismissADView];
}
}
下面是图片数据获取的代码
/**
图片数据获取
*/
-(void)imageDataWihtUrl:(NSString *)imageUrl callback:(void(^)(HHRADPictureType type,id data))callback{
NSString *filePath = [self filePathWihtImagePath:imageUrl];
HHRADPictureType type_p = HHRADPictureJPG;
//如果指定资源文件类型(推荐使用)
if (_adModel.type_str) {
if ([_adModel.type_str containsString:@"gif"] || [_adModel.type_str containsString:@"GIF"]) {
type_p = HHRADPictureGIF;
}
}else{
//否则按默认的类型判断(链接不是文件类型结尾的可能会出现显示bug)
if ([filePath hasSuffix:@"gif"] || [filePath hasSuffix:@"GIF"]) {
type_p = HHRADPictureGIF;
}
}
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
//如果存在则加载缓存数据
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
callback(type_p,imageData);
}else{
//否则加载网络数据
callback(type_p,imageUrl);
}
}
以上只是部分代码,详情请看github demo实现,同时欢迎对代码实现上不好的地多多指出。
网友评论