app启动广告页的实现,解决了广告图片要实时更新的问题

本文介绍了一种解决App启动页广告无法实时更新问题的方法。通过在启动时预加载图片,并设定超时机制,确保后台更新的广告能即时展示。代码示例使用iOS平台的Swift语言实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上很多的实现方法很多都是显示第一次的缓存的图片,这样就造成后台更新广告图片App不能实时展示的问题。

我的具体实现思路是: 1.启动时先获取启动页的图片全屏展示。 2.设计一个等待时间,如果超过等待时间还没拿到图片就把获取的启动页去掉,否则就显示广告页。

具体代码的实现:

@interface SSLaunchAdView : UIView

@property (nonatomic, strong) UIImageView *imageView;
///图片的下载链接
@property (nonatomic, strong) NSString *imageUrl;
///广告的显示时间
@property (nonatomic, assign) NSInteger AdTime;
///要等待的时间
@property (nonatomic, assign) NSInteger waitTime;


@property (nonatomic, copy) void(^clickImageView)(SSLaunchAdView *view);
- (id)initWithFrame:(CGRect)frame
             AdTime:(NSInteger)AdTime
           waitTime:(NSInteger)waitTime
             onView:(UIView *)onView;
///点击广告响应
- (void)clickImageView:(void(^)(SSLaunchAdView *view))block;
///删除广告页
- (void)hideView;
@end

#import "SSLaunchAdView.h"
#import "SDWebImageManager.h"

@interface SSLaunchAdView ()

@property (nonatomic, strong) UIImageView *bg_imageView;
@property (nonatomic, strong) NSTimer *myTimer;
@property (nonatomic, assign) NSInteger timeCount;
@property (nonatomic, strong) UIButton *button;

@property (nonatomic, assign) BOOL isOutWaitTime;
@property (nonatomic, assign) BOOL isHasImage;
@end

@implementation SSLaunchAdView

- (id)initWithFrame:(CGRect)frame AdTime:(NSInteger)AdTime waitTime:(NSInteger)waitTime onView:(UIView *)onView{
    if (self = [super initWithFrame:frame]) {
        self.AdTime = AdTime;
        self.waitTime = waitTime;
      
        [onView addSubview:self];
    
      
    
        _bg_imageView = [UIImageView new];
        [self addSubview:_bg_imageView];
        [self->_bg_imageView setImage:[UIImage imageNamed:[self getLaunchImageName]]];
       
    
        _imageView = [UIImageView new];
//        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_imageView];

        
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setTitle:@"跳过" forState:0];
        _button.titleLabel.font = [UIFont systemFontOfSize:12];
        [_button setTitleColor:[UIColor whiteColor] forState:0];
        _button.backgroundColor = [UIColor blackColor];
        [self addSubview:_button];
        [_button setHidden:YES];
    
        [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapImageView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView)];
        [_imageView addGestureRecognizer:tapImageView];
        
        if (waitTime>0) {
            [self performSelector:@selector(waitAction) withObject:nil afterDelay:waitTime+0.2];
        }
    }
    
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, 0, CGRectGetWidth(self.superview.frame), CGRectGetHeight(self.superview.frame));
    _bg_imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.superview.frame), CGRectGetHeight(self.superview.frame));
    _imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.superview.frame), CGRectGetHeight(self.superview.frame));
    if (@available(iOS 11.0, *)){
        _button.frame = CGRectMake(CGRectGetWidth(_imageView.frame)-50-20, _button.superview.safeAreaInsets.top+20, 50, 28);
    }else{
        _button.frame = CGRectMake(CGRectGetWidth(_imageView.frame)-50-20, 37, 50, 28);
    }
}


- (void)buttonAction:(UIButton *)button{
    [self hideView];
}

- (void)waitAction{
    @synchronized (self) {
        if (!self.isHasImage) {
            self.isOutWaitTime = YES;
            [self hideView];
        }
    }
   
}

- (void)tapImageView{
//    [self hideView];
    
    if (self.clickImageView) {
        self.clickImageView(self);
    }
}


- (NSString *)getLaunchImageName
{
    CGSize viewSize = [[UIScreen mainScreen] bounds].size;
    // 竖屏
    NSString *viewOrientation = @"Portrait";
    NSString *launchImageName = nil;
    NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary* dict in imagesDict)
    {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
        {
            launchImageName = dict[@"UILaunchImageName"];
        }
    }
    return launchImageName;
}

- (void)setImageUrl:(NSString *)imageUrl{
    _imageUrl = imageUrl;
    if (!_imageUrl) {
        [self hideView];
        return;
    }
    __weak typeof(self) weakSelf = self;
    [[SDWebImageManager sharedManager]loadImageWithURL:[NSURL URLWithString:_imageUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {

    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        __strong typeof(self) strongSelf = weakSelf;
        if (image&&finished&&!self.isOutWaitTime) {
            strongSelf.imageView.image = image;
            strongSelf.isHasImage = YES;
            [strongSelf addTimer];
        }
    }];
}

- (void)hideView{
    [self removeTimer];
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)addTimer{
    
    _myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:_myTimer forMode:NSRunLoopCommonModes];
    if (self.imageView.image) {
        [_button setHidden:NO];
    }
}

- (void)removeTimer{
    [_myTimer invalidate];
    _myTimer = nil;
}

- (void)timerAction{
    _timeCount ++;
    if (_timeCount==self.AdTime) {
        [self hideView];
    
    }else{
        [_button setTitle:[NSString stringWithFormat:@"%ld跳过",self.AdTime-_timeCount] forState:0];
    }
}

- (void)clickImageView:(void (^)(SSLaunchAdView *))block{
    self.clickImageView = block;
}


复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值