iOS App启动广告

一般情况下,App在每次启动时都会有广告,那么是自己自定义UIViewController来实现还是直接使用LaunchScreen.storyboard来实现呢?


因LaunchScreen的本质是屏幕截屏,所以苹果不允许修改LaunchScreen对应的自定义类Custom Class, 因在启动广告时还有倒数3秒钟跳过的功能,还有点击广告会打开safari浏览器的功能,不能绑定自定义的类就不能处理这些业务逻辑, 所以通过LaunchScreen在启动时实现启动广告是行不通的。所以只能通过自定义UIViewController来实现。
此时有两种方案:
①:程序一启动就进入广告界面,将窗口的根控制器设置为广告控制器(采用该方式,该方式简单)
②:窗口的根控制器仍为TabBarController,在窗口上加一个广告界面挡住tabBar界面, 3秒钟后移除广告界面(该方式稍微复杂)

引导页只有在应用程序第一次打开时才会出现,而广告页一般是应用程序每次启动都会打开,我们可以这样应用程序第一次启动时将rootViewController = 引导页,不是第一次启动根视图控制器设置为广告页。

在应用程序启动完成后,进入广告界面,广告界面由UIImageView + UIView + UIButton组成,UIImageView用于存放启动图片,UIView作为容器视图以便将来添加广告图片,UIButton用于跳过广告。UIImageView在最底层,UIView容器视图将在UIImageView的正上方,完全遮挡住UIImageView,UIButton是UIView的子视图。为什么还要显示启动图片,进入广告之后,直接加载广告图片不就行了吗?原因有二:一是在请求网络图片之前广告界面是空的或者网络比较慢时也是一篇空白,不太友好,第二如果图片是启动图片,因启动时的图片,和广告的背景图片是同一张图片,这样可以给用户造成一种假象:用户会认为程序还在启动,还没结束。因为启动图片一直没有消失,其实已经消失过了,又加载了一张启动图片。

当在Assets.scassets中看到图片正下方出现Unassigned,说明图片不能被识别,这时候就不要把图片放到Assets中了,直接放到项目中的某个文件夹下如Resource–>Image文件夹或Supporting Files中即可。Assets的目的是让图片更加安全,防止别人盗取图片。

这里写图片描述

最终代码

步骤一:因界面比较简单适合使用XIB搭建广告界面

iOS App启动广告_第1张图片

步骤二:应用程序启动时将窗体的根视图控制设置为广告

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 1. 创建窗口--------------------------------------------------
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 2. 设置窗口的根试图控制器--------------------------------------
    BWAdViewController *adViewController = [[BWAdViewController alloc] init];
    self.window.rootViewController = adViewController;

    // 3. 显示窗口--------------------------------------------------
    [self.window makeKeyAndVisible];

    return YES;
}

步骤三:加载广告图片,处理逻辑

模型由广告的图片、点击广告图片所对应的地址,以及广告图片对应的宽度和高度
Ad广告model

#import 

@interface BWAd : NSObject

/** 广告地址 */
@property (strong, nonatomic) NSString *w_picurl;

/** 广告跳转页面 */
@property (strong, nonatomic) NSString *ori_curl;

/** 图片宽高 */
@property (assign, nonatomic) CGFloat w;
@property (assign, nonatomic) CGFloat h;

@end

BWAdViewController

#import "BWAdViewController.h"
#import "BWAd.h"
#import "BWTabBarController.h"

#import 
#import 
#import 

#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface BWAdViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (weak, nonatomic) IBOutlet UIView *adContainView;
@property (weak, nonatomic) IBOutlet UIButton *jumpButton;
@property (weak, nonatomic) UIImageView *adImageView;

@property (strong, nonatomic) BWAd *ad;
@property (weak, nonatomic) NSTimer *timer;
@end

@implementation BWAdViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置最底层的图片为启动图片,让用户认为程序还在启动的假象,进行屏幕适配
    NSString *launchImage = nil;
    if (isIphone6p) {
        launchImage = @"LaunchImage-800-Portrait-736h@3x";
    } else if (isIphone6) {
        launchImage = @"LaunchImage-800-667h";
    } else if (isIphone5) {
        launchImage = @"LaunchImage-568h";
    } else if (isIphone4) {
        launchImage = @"LaunchImage-700";
    }
    self.launchImageView.image = [UIImage imageNamed:launchImage];

    // 加载广告数据
    [self loadAdData];
}

- (void)loadAdData {
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = code2;
    [manger GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        NSDictionary *adDict = [responseObject[@"ad"] lastObject];
        _ad = [BWAd mj_objectWithKeyValues:adDict];
        CGFloat h = ScreenWidth / _ad.w * _ad.h; // 按比例拉伸
        // 懒加载并设置广告的frame
        self.adImageView.frame = CGRectMake(0, 0, ScreenWidth, h);
        [self.adImageView sd_setImageWithURL:[NSURL URLWithString:_ad.w_picurl]];

        // 启动定时器,定时倒数数秒跳过广告
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BWLog(@"error:%@", error);
    }];
}

// 定时改变标题,数到0时进入主框架并关闭定时器
- (void)timeChange {
    static int i = 3;
    [_jumpButton setTitle:[NSString stringWithFormat:@"(%d秒)跳转", i] forState:UIControlStateNormal];
    if (i == 0) {
        // 销毁广告界面, 进入主框架界面
        [self jumpAction:nil];
    }
    i--;
}

- (UIImageView *)adImageView {
    if (_adImageView == nil) {
        UIImageView *adImageView = [[UIImageView alloc] init];
        [self.adContainView insertSubview:adImageView atIndex:0];

        adImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adDetail)];
        [adImageView addGestureRecognizer:tap];

        _adImageView = adImageView;
    }

    return _adImageView;

}


- (IBAction)jumpAction:(id)sender {
    [UIApplication sharedApplication].keyWindow.rootViewController = [[BWTabBarController alloc] init];

    // 销毁定时器
    [_timer invalidate];

}

- (void)adDetail {
    NSURL *url = [NSURL URLWithString:_ad.ori_curl];
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }
}
@end

实现效果:
iOS App启动广告_第2张图片

[UIApplication sharedApplication].keyWindow == [[[UIApplication sharedApplication] delegate] window];

你可能感兴趣的:(iOS)