美文网首页以后备用技术
ios 启动添加广告页

ios 启动添加广告页

作者: lizhi_boy | 来源:发表于2018-06-19 11:38 被阅读170次

由于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;
    }
}

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

相关文章

  • ios 启动添加广告页

    由于ios启动页是有苹果的规范的,所以动态的修改启动页可能会有被拒的风险。关于这一点我没试过,如果想做是可以做的,...

  • iOS 启动页广告详解

    现在的项目中, 添加广告越来越普遍, 那么在启动页添加广告就是重中之重, 下面是我开发过程中的经验. 添加启动页广...

  • 应用添加广告启动页

    通常一个app启动的时候会加载一个广告启动页。如下图: 不啰嗦直接上代码 首先创建QBYADViewControl...

  • iOS 启动页广告

    思路1.判断广告文件是否存在,以及初始化广告位2.无论沙盒中是否存在广告图片,都需要重新调用获取广告接口,判断广告...

  • 收集一些好用的第三方库

    1.iOS设置启动页后的广告页: https://github.com/CoderZhuXH/XHLaunchAd...

  • iOS适配启动页

    iOS适配启动页 iOS适配启动页

  • 这就是 封装  iOS 启动页

    iOS 启动广告页思路,直接 push,首页不出现,上海华新镇风味 PM 提了个需求,启动广告界面点击了, 就直接...

  • 关于启动页添加广告的一些想法

    闲来无事,封装了一个广告页,还算比较实用,首先我们要知道如何来添加这个广告,以及添加在哪里,启动页上都有什么,逻辑...

  • iOS启动广告页展示

    iOS启动广告页展示 现在很多主流App如:淘宝、美团等在启动过程中都会展示广告页。在这些流量巨大的App中展示广...

  • iOS启动页广告加载

    最近项目里有一个广告启动页的需求跟进,就是现在很多App都会有的启动页广告。我首先想到的自然是去github上找现...

网友评论

    本文标题:ios 启动添加广告页

    本文链接:https://www.haomeiwen.com/subject/xddzhftx.html