標籤:
現在很多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那就猛戳這裡
文/七秒記憶的魚兒(簡書作者)
原文連結:http://www.jianshu.com/p/6d613c957a36
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。
iOSAPP啟動時實現載入廣告