现在很多APP在启动的时候都在加载广告,现在也很流行,主要是盈利啊。笔者也做了很多关于广告的事情。现在记录下自己在APP启动的时候,怎么加载广告的。
下面总结下广告加载的三种方式
1.现在很多APP的广告不是在程序启动的时候开始请求广告的,而是直接加载广告的图片链接。可以在程序启动完后给个特定的时间去请求广告,并缓存到数据库。广告平台也是建议这样做的,可以节省用户的打开程序的时间。但是这种方法是有缺点的,就是广告的时常,一般的广告的平台都是55分钟左右展示是有效的。(如果有自己的广告商,还是建议这样使用)
2.直接在启动的时候才开始去请求广告,并把广告加载出来,这个不用考虑广告的时常,但是要考虑APP的启动的时间,这样会有两个网络的请求,会比较耗时(笔者的APP也是这样的效果,我写的demo也是这样的效果)
3.在APP启动的时候直接加载广告的图片,图片是上次启动请求广告,并将广告保存到本地的(主要实用:自己有广告商)
先看下笔者的效果
Untitled.gif其实这个上面笔者是直接present一个控制器来显示广告,当广告显示完成之后,直接dis掉的,只是把他的效果给自定义了
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
_imageView.image = [UIImage imageNamed:@"6"];
[self.view addSubview:_imageView];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 50)];
label.text = @"七秒记忆鱼儿注,转注请署名七秒记忆的鱼儿";
label.textColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
#warning 注意 必须盖住
[self.view bringSubviewToFront:_imageView];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
OneViewController *one = [[OneViewController alloc]init];
one.myBlock = ^(BOOL isClick){
_isClick = isClick;
[_imageView removeFromSuperview];
_imageView = nil;
};
if (_isClick == NO) {
[self presentViewController:one animated:NO completion:nil];
}
}
主要的操作还是实现了两个定时器来操作的,_waitRequestTimer是用来判定请求广告时间的,大于多少后直接就进入APP,_adsAccordingTimer这个定时器用来显示广告的,如果超出一定的时间加载广告也会进入APP
- (void)viewDidLoad {
[super viewDidLoad];
self.transitioningDelegate = self;
_LaunchImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
_LaunchImage.image = [UIImage imageNamed:@"6"];
[self.view addSubview:_LaunchImage];
[self requestAds];
_seconds = 6;
_waitRequestTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(Waiting) userInfo:nil repeats:YES];
}
-(void)requestAds
{
// 模拟网络请求
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self addUI];
[_waitRequestTimer invalidate];
});
}
-(void)Waiting
{
_seconds--;
if (_seconds == 0) {
[self myLog];
}
}
/**
* 添加广告上面的UI
*/
-(void)addUI{
_adsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, 0.87*height)];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://i.l.inmobicdn.net/banners/FileData/290057e6-a662-411d-86bb-688b3c284460.jpeg"]];
UIImage *main_image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
_adsImageView.image = main_image;
});
});
[_LaunchImage addSubview:_adsImageView];
/**
* 显示倒计时的时间按钮
*
*/
_time_btton = [self addButtonWithImagename:@"miaoshu" andTitle:@"5S" andFram:CGRectMake(width-70, 30, 50, 30)];
/**
* 创建倒计时
*
*/
_adsAccordingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAccord) userInfo:nil repeats:YES];
_adsAccording_Integer = 6;
}
/**
* 广告的倒计时
*/
-(void)timerAccord
{
_adsAccording_Integer--;
[_time_btton setTitle:[NSString stringWithFormat:@"%zd",_adsAccording_Integer] forState:0];
if (_adsAccording_Integer <= 0) {
[self myLog];
}
/**等于2秒时显示直接接入按钮*/
if (_adsAccording_Integer == 2) {
_jump_button = [self addButtonWithImagename:@"tiaoguo" andTitle:@"直接进入>" andFram:CGRectMake(width-150,height*0.83-60, 120, 45)];
[_jump_button setTitleColor:[UIColor whiteColor] forState:0];
_jump_button.titleLabel.font = [UIFont systemFontOfSize:15];
[_jump_button addTarget:self action:@selector(myLog) forControlEvents:UIControlEventTouchUpInside];
}
}
/**
* 点击进入按钮
*/
-(void)myLog
{
self.myBlock(YES);
[self dismissViewControllerAnimated:YES completion:^{
[_waitRequestTimer invalidate];
[_adsAccordingTimer invalidate];
}];
}
/**
* 创建广告上面的button按钮
*
* @param imageName 按钮的图片
* @param title 按钮的文字
* @param btnFram 按钮的fram
*
* @return 返回按钮
*/
-(UIButton *)addButtonWithImagename:(NSString *)imageName andTitle:(NSString *)title andFram:(CGRect)btnFram{
UIButton *button =[[UIButton alloc]initWithFrame:btnFram];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:0];
[button setTitle:title forState:0];
button.titleLabel.font = [UIFont systemFontOfSize: 12.0];
[button setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
[_adsImageView addSubview:button];
return button;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
return [AAPLCrossDissolveTransitionAnimator new];
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
return [AAPLCrossDissolveTransitionAnimator new];
}
下载完整的demo那就猛戳这里
网友评论
这是为什么