现在的项目中, 添加广告越来越普遍, 那么在启动页添加广告就是重中之重, 下面是我开发过程中的经验.
添加启动页广告的步骤
1.先创建一个UIViewController作为广告页.
2.请求广告.
3.显示广告.
4.缓存这次的广告信息.
5.下一次优先显示上次缓存的广告信息.
创建UIViewController
@interface UserBaseInfoVC : UIViewController
@end
- 在显示广告前, 应用启动页图片来代替广告, 这样就会给用户造成一个假象, 以为还在启动页.
#pragma mark - 启动页
- (void)setScreenForLauchScreen {
self.LaunchImageView = [[UIImageView alloc] init];
[self.view addSubview:self.LaunchImageView];
if ([UIScreen mainScreen].bounds.size.height == 736) {
self.LaunchImageView.image = [UIImage imageNamed:@"736"];
}else if([UIScreen mainScreen].bounds.size.height == 667){
self.LaunchImageView.image = [UIImage imageNamed:@"667"];
}else if([UIScreen mainScreen].bounds.size.height == 568){
self.LaunchImageView.image = [UIImage imageNamed:@"568"];
}else if ([UIScreen mainScreen].bounds.size.height == 480){
self.LaunchImageView.image = [UIImage imageNamed:@"480"];
}else if ([UIScreen mainScreen].bounds.size.height == 812){
self.LaunchImageView.image = [UIImage imageNamed:@"812"];
}else if ([UIScreen mainScreen].bounds.size.height == 896){
self.LaunchImageView.image = [UIImage imageNamed:@"896"];
}
[self.LaunchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
}
- 请求广告
#pragma mark - 获取广告信息
- (void)getAdListData {
[AdViewModel getAdDataSuccess:^(AdListModel * _Nonnull adList) {
NSLog(@"获取广告信息 UserBaseInfoVC");
if (adList) {
//赋值
[[SaveDataTools shareSaveDataTools] saveAdList:adList];
}
} fail:^(NSError * _Nonnull error) {
}];
}
- 显示广告
UIImageView *adImageView = [[UIImageView alloc] init];
adImageView.userInteractionEnabled = YES;
int arc4random = kArc4random([[SaveDataTools shareSaveDataTools] getAdList].tdList3.count);
AdModel *adModel = [[SaveDataTools shareSaveDataTools] getAdList].tdList3[arc4random];
__weak typeof(self) weakSelf = self;
[self.view addSubview:adImageView];
[adImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.mas_equalTo(0);
make.size.mas_equalTo(CGSizeMake(kScrentW, kScrentH - 60 - 30 - 30));
}];
//添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAdImageViewTap)];
[adImageView addGestureRecognizer:tap];
//加载图片
[adImageView sd_setImageWithURL:[NSURL URLWithString:adModel.image_url] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//隐藏启动页
weakSelf.LaunchImageView.hidden = YES;
//压缩图片
image = [image scaleToWidth:kScrentW];
if (image.size.height > (kScrentH - 120)) {
adImageView.contentMode = UIViewContentModeCenter;
}
adImageView.image = image;
//广告标记
UIImageView *adtab = [[UIImageView alloc] init];
adtab.image = [UIImage imageNamed:@"广告"];
[weakSelf.view addSubview:adtab];
[adtab mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(adImageView);
make.bottom.equalTo(adImageView);
}];
//底部
UIImageView *bootomImageView = [[UIImageView alloc] init];
bootomImageView.image = [UIImage imageNamed:@"启动页-icon"];
[weakSelf.view addSubview:bootomImageView];
[bootomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.bottom.equalTo(self.view).offset(-30);
make.size.mas_equalTo(CGSizeMake(173, 60));
}];
//赋值
weakSelf.adModel = adModel;
}];
- 缓存广告信息
#pragma mark - 获取广告信息
- (void)getAdListData {
[AdViewModel getAdDataSuccess:^(AdListModel * _Nonnull adList) {
NSLog(@"获取广告信息 UserBaseInfoVC");
if (adList) {
//赋值
[[SaveDataTools shareSaveDataTools] saveAdList:adList];
}
} fail:^(NSError * _Nonnull error) {
}];
}
- 到这基本上大功告成了.
网友评论