以后备用技术

ios 启动添加广告页

2018-06-19  本文已影响170人  lizhi_boy

由于ios启动页是有苹果的规范的,所以动态的修改启动页可能会有被拒的风险。关于这一点我没试过,如果想做是可以做的,毕竟runtime是很强大。
废话不多说了。。。。。

既然启动页修改比较麻烦,那就在启动页结束后再显示广告页,这个方式好像很多app都是采用了。


大概思路是这个吧.png

这个思路很简单,那么我们看看广告页有什么结构:

1、广告图片(图片,GIF,视频)等 
2、跳过按钮(计时器)

目前支持图片jpg 和 gif的格式的广告,后续会补充其他格式。(gif显示基于sdWebImage框架)
目前的功能点

1、图片做了缓存操作
缓存思路是:根据图片的链接作为文件名称,启动的时候回比对服务端的图片链接,如若有变,则保存新的图片作为广告,如果不变则拿缓存的图片作为广告页。
2、定时自定义可随意设置广告显示的时间
3、每天广告显示次数限制(如果一直启动显示广告,用户会很反感的);

图片缓存代码:

-(void)loadData{

    NSString *imagePath = _sources;
    __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.adImageView.image = image;
              
            }else{
                weakSelf.adImageView.image = [UIImage imageWithData:data];
            }
        }else if([data isKindOfClass:[NSString class]]){
            
            dispatch_queue_t globalqueue = dispatch_get_global_queue(0, 0);
            dispatch_async(globalqueue, ^{
                NSData *image_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:data]];
                if ([image_data writeToFile:[self filePathWihtImagePath:data] atomically:YES]) {
                    NSLog(@"广告页缓存成功");
                }else{
                    NSLog(@"广告页缓存失败");
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    if (type == HHRADPictureGIF) {
                        
                        UIImage *image = [UIImage sd_animatedGIFWithData:image_data];
                        weakSelf.adImageView.image = image;
                    }else{
                         weakSelf.adImageView.image = [UIImage imageWithData:image_data];
                    }
                    
                });
            });
        }else{
            //后续补充其他类型
        }
    }];
}


/**
 缓存路径
 @param imagePath 图片链接
 @return 图片路径
 */
-(NSString *)filePathWihtImagePath:(NSString *)imagePath{
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *filePath = [cachePath stringByAppendingPathComponent:imagePath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //下面这个方法很关键直接影响文件缓存
   [fileManager createDirectoryAtPath:[filePath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];//
    return filePath;
}


/**
 图片数据
 */
-(void)imageDataWihtUrl:(NSString *)imageUrl callback:(void(^)(HHRADPictureType type,id data))callback{
    
    NSString *filePath = [self filePathWihtImagePath:imageUrl];
    
    HHRADPictureType type_p = HHRADPictureJPG;
    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);
    }
}


-(void)dismissADView{
    //清除计时器和视图
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.alpha = 0.0;
    } completion:^(BOOL finished) {
        dispatch_cancel(weakSelf.ad_timer);
        [weakSelf removeFromSuperview];
    }];

}


倒计时相关代码
/**
 设置倒计时
 @param timeLine 倒计时时间
 */
-(void)countDownTime:(NSInteger)timeLine{
    
    __block int timeOut = (int)timeLine;
    __weak typeof(self) weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.ad_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_ad_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC,0);
    dispatch_source_set_event_handler(_ad_timer, ^{
        if (timeOut <= 0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf dismissADView];
            });
        }else{
           //显示秒数
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.skipBtn setTitle:[NSString stringWithFormat:@"%ldS 跳过",(long)timeOut] forState:0];
            });
            timeOut--;
        }
    });
    dispatch_resume(_ad_timer);
}


//次数限制代码
-(BOOL)saveTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    NSString *launchDate = [[NSUserDefaults standardUserDefaults] valueForKey:@"LaunchDate"];
    
    
    if (![launchDate isEqualToString:dateTime]) {
         [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"LaunchTimes"];
    }
    
    NSInteger timesNum = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchTimes"];
    if (self.times == 0) {
        self.times = 99999;
    };
    
    if (timesNum < self.times) {
        timesNum ++;
        [[NSUserDefaults standardUserDefaults] setInteger:timesNum forKey:@"LaunchTimes"];
        [[NSUserDefaults standardUserDefaults] setValue:dateTime forKey:@"LaunchDate"];
        return YES;
        
    }else{
        return NO;
    }
}

总体代码上还是比较少的,同时逻辑也是很通俗易懂的,如果有些地方写的不够好,尽请见谅。

上一篇 下一篇

猜你喜欢

热点阅读