iOS 启动页广告

iOS 启动页广告

前段时间,公司要我写一个启动页广告,很简单,这里就说说怎么写启动页广告.
具体思路:因为加载图片需要时间,所以说第一次运行app的时候不显示图片,加载完成后,下载图片保存到本地.等第二次加载app的时候,启动完成后加载本地图片,这样启动后就可以直接显示广告图片.

效果图效果图

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}

app启动完成的时候调用的方法,所以在这里把广告也加载在window上

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  adview = [[ZFLADView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
                adview.backgroundColor = [UIColor whiteColor];
                adview.imageFilepath = filePath;
                adview.timeCount = timeDuration/1000;
                adview.linkUrl = linkUrl;
                [adview showAD];
                }

下载图片的方法:
这是一个包装好的从接口请求参数,然后下载图片到本地的方法:

- (void)requestAdvertistInfo{

    [ShoppingService shopServicePost:@"/boot.html" parameters:nil block:^(NSDictionary *result, NSError *error) {
        if (error) {
            [self deleteOldADImage];
        }
        if([[result objectForKey:@"code"]isEqualToString:@"success"]){
            NSDictionary    *modelInfo = [result objectForKey:@"model"];
            NSArray         *dataArray = [modelInfo arrayForKey:@"screen"];
            NSString        *imgurl = [dataArray[0] stringForKey:@"imgUrl"];
            NSString        *linkUrl = [dataArray[0] stringForKey:@"linkUrl"];
            NSInteger       timeDuration = [dataArray[0] integerForKey:@"duration"];
            //获取图片名称/路径
            NSArray         *stringArr = [imgurl componentsSeparatedByString:@"/"];
            NSString        *imageName = stringArr.lastObject;
            NSString        *filePath = [self getFilePathFromImageName:imageName];
            BOOL            isExist = [self isFileExistWithFilePath:filePath];
            if (!isExist) {
                //加载图片
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                    NSData      *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgurl]];
                    UIImage     *image = [UIImage imageWithData:data];
                    [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
                    if(imgurl){
                        if([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]){
                            //删除以前保存的照片
                            [self deleteOldADImage];
                            NSDictionary *adInfo = @{@"imageName":imageName,@"duration":@(timeDuration),@"linkUrl":linkUrl};
                            NSUserDefaults *userdefult = [NSUserDefaults standardUserDefaults];
                            [userdefult setObject:adInfo forKey:kAdvertiseInfo];
                            [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
                            [userdefult synchronize];
                        }else{
                            return ;
                        }
                    }else{
                        [self deleteOldADImage];
                    }
                });
            }
        }
    }];
}

得到图片路径

- (NSString *)getFilePathFromImageName:(NSString *)imageName{
    if (imageName) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName];
        return filePath;
    }
    return nil;
}

判断文件是否存在

- (BOOL)isFileExistWithFilePath:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = FALSE;
    return [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
}

删除存在的旧文件

- (void)deleteOldADImage{
    //删除以前保存的照片
    NSString *oldFile = [[[NSUserDefaults standardUserDefaults] objectForKey:kAdvertiseInfo] stringForKey:@"filePath"];
    NSString *userName = [[[NSUserDefaults standardUserDefaults] objectForKey:kAdvertiseInfo] stringForKey:@"imageName"];

    if (oldFile) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *oldFilepath = [self getFilePathFromImageName:userName];
        [fileManager removeItemAtPath:oldFilepath error:nil];
    }
}

广告页面:

//
//  ZFLADView.m
//  zhefengle
//
//  Created by zmm on 16/6/15.
//  Copyright © 2016年 vanwell. All rights reserved.
//

#import "ZFLADView.h"
#import "LogicHandler.h"
#import "ZFLMainViewController.h"
@interface ZFLADView (){
    UIImageView *_adImageView;
    UIButton    *_countButton;
    NSInteger   _count;//计数
    NSTimer     *_countTimer;
}

@end

@implementation ZFLADView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

- (void)createUI{
    _adImageView = [[UIImageView alloc]initWithFrame:self.frame];
    _adImageView.contentMode = UIViewContentModeScaleAspectFit;
    _adImageView.clipsToBounds = YES;
    [self addSubview:_adImageView];

    CGFloat btnWidth = 60;
    CGFloat btnheight = 30;
    _countButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _countButton.frame = CGRectMake(kScreenWidth - btnWidth - 24, btnheight, btnWidth, btnheight);
    [_countButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    _countButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_countButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _countButton.backgroundColor = [UIColor colorWithRed:38 /255.0 green:38 /255.0 blue:38 /255.0 alpha:0.6];
    _countButton.layer.cornerRadius = 4;
    [self addSubview:_countButton];
    UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushToAD)];
    [self addGestureRecognizer:tapImage];
}

- (void)pushToAD{

    //需要写跳转的方法
        NSUserDefaults *userdefaullt = [NSUserDefaults standardUserDefaults];
        NSDictionary *infoDic = [userdefaullt objectForKey:kAdvertiseInfo];
        NSString *linkUrl = [infoDic stringForKey:@"linkUrl"];
        if (linkUrl) {//这里是根据url跳转至safari
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:linkUrl]];
        }
}
//通过传入的文件路径,设置图片
- (void)setImageFilepath:(NSString *)imageFilepath{
    _imageFilepath = imageFilepath;
    [_adImageView setImage:[UIImage imageWithContentsOfFile:imageFilepath]];
}
//倒计时
- (void)setTimeCount:(NSInteger)timeCount{
    _timeCount = timeCount;
    [_countButton setTitle:[NSString stringWithFormat:@"跳过%ld", (long)_timeCount] forState:UIControlStateNormal];

}
//图片消失
- (void)dismiss{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.f;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
//显示广告页面
- (void)showAD{
    [self statTimer];
    UIWindow *window = [UIApplication sharedApplication].keyWindow ;
    [window addSubview:self];
}
//开始倒计时
- (void)statTimer{
    _count = _timeCount; 
    [[NSRunLoop mainRunLoop] addTimer:[self countTime] forMode:NSRunLoopCommonModes];
}

- (NSTimer *)countTime{
    if (!_countTimer) {
        _countTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countTimeDown) userInfo:nil repeats:YES];
    }
    return _countTimer;
}

- (void)countTimeDown{
    _count--;
    [_countButton setTitle:[NSString stringWithFormat:@"跳过%ld",(long)_count] forState:UIControlStateNormal];
    if (_count == 0) {
        [_countTimer invalidate];
        _countTimer = nil;
        [self dismiss];
    }

}
@end

重要的代码都放在上面了,有些其他多余的影响阅读的代码,抱歉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值