通常一个app启动的时候会加载一个广告启动页。如下图:
不啰嗦直接上代码
- 首先创建QBYADViewController类继承UIViewController并且勾选创建xib文件。因为界面比较简单所以用xib描述。
界面如下图:
launch Image view 以后放启动图片,防止广告页加载失败。
Ad Contain View 是占位用的,放广告页用。
Jump Btn 是跳过按钮。
为什么这么设计呢?
1.广告业务逻辑
2.占位视图思想:有个控件不确定尺寸,但是层次结构已经确定,就可以使用占位视图思想
3.屏幕适配.通过屏幕高度判断
- 接着就是 QBYADViewController中的代码
objc
- (void)viewDidLoad {
[super viewDidLoad];
// 设置启动图片
[self setupLaunchImage];
// 加载广告数据 => 拿到活时间 => 服务器 => 查看接口文档 1.判断接口对不对 2.解析数据(w_picurl,ori_curl:跳转到广告界面,w,h) => 请求数据(AFN)
[self loadAdData];
// 创建定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
其中设置启动图片为防止图片广告图片加载不出来。
```objc```
/***********屏幕适配*************/
#define QBYScreenW [UIScreen mainScreen].bounds.size.width
#define QBYScreenH [UIScreen mainScreen].bounds.size.height
#define iphone6P (QBYScreenH == 736)
#define iphone6 (QBYScreenH == 667)
#define iphone5 (QBYScreenH == 568)
#define iphone4 (QBYScreenH == 480)
/***********屏幕适配*************/
// 设置启动图片
- (void)setupLaunchImage
{
// 6p:LaunchImage-800-Portrait-736h@3x.png
// 6:LaunchImage-800-667h@2x.png
// 5:LaunchImage-568h@2x.png
// 4s:LaunchImage@2x.png
if (iphone6P) { // 6p
self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
} else if (iphone6) { // 6
self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
} else if (iphone5) { // 5
self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-568h"];
} else if (iphone4) { // 4
self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-700"];
}
}
- 加载广告页 新建一个数据模型类QBYADItem
objc
-
(void)loadAdData
{
// unacceptable content-type: text/html" 响应头// 1.创建请求会话管理者
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];// 2.拼接参数
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"code2"] = code2;// 3.发送请求
[mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php?code2=phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
// [responseObject writeToFile:@"/Users/gujunqi/Desktop/ad.plist" atomically:YES];
// 请求数据 -> 解析数据(写成plist文件) -> 设计模型 -> 字典转模型 -> 展示数据
// 获取字典
NSDictionary *adDict = [responseObject[@"ad"] lastObject];
// 字典转模型
_item = [QBYADItem mj_objectWithKeyValues:adDict];
// 创建UIImageView展示图片 =>
CGFloat h = QBYScreenW / _item.w * _item.h;
self.adView.frame = CGRectMake(0, 0, QBYScreenW, h);
// 加载广告网页
[self.adView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
- 其余代码,很好理解
```objc```
- (void)timeChange
{
// 倒计时
static int i = 10;
if (i == 0) {
[self clickJump:nil];
}
i--;
// 设置跳转按钮文字
[_jumpBtn setTitle:[NSString stringWithFormat:@"跳转 (%d)",i] forState:UIControlStateNormal];
}
// 点击跳转做的事情
- (IBAction)clickJump:(id)sender {
// 销毁广告界面,进入主框架界面
QBYTabBarController *tabBarVc = [[QBYTabBarController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
// 干掉定时器
[_timer invalidate];
}
- (UIImageView *)adView
{
if (_adView == nil) {
UIImageView *imageView = [[UIImageView alloc] init];
[self.adContainView addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
[imageView addGestureRecognizer:tap];
imageView.userInteractionEnabled = YES;
_adView = imageView;
}
return _adView;
}
// 点击广告界面调用
- (void)tap
{
// 跳转到界面 => safari
NSURL *url = [NSURL URLWithString:_item.ori_curl];
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:url]) {
[app openURL:url];
}
}
代码和资料具体下载地址:
https://github.com/qibingying/iosDevelopInsight/tree/master/%E5%B9%BF%E5%91%8A%E5%90%AF%E5%8A%A8%E9%A1%B5