需求:在启动的时候,一般会去先加载广告界面,需要一个自定义类去管理这个广告界面
分析:
(1) 若使用系统本身的LaunchScreen.storyboard
,就无法使用自定义的类
(2) 广告业务逻辑
(3) 占位视图思想:有个控件不确定尺寸,但是层次结构已经确定,就可以使用占位视图思想
(4) 屏幕适配.通过屏幕高度判断
报错: unacceptable content-type: text/html"
原因: 响应头问题,AFN发送网络请求,返回的数据是 text/html类型,AFN不能够接收
解决:manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
拓展:为什么使用static来修饰int
原因:因为这是全局的栈,只分配一次内存,若不用static修饰,会每次调用的时候就分配一次内存。
方案解析:设置窗口的根控制器为广告控制器
(1)xib的描述(3层):
第一层:启动图片
第二层:占位图片(UIView--->UIimageView)
第三层:Button(与占位图平级)
(2)代码如下:
pch里,屏幕适配
/***********屏幕适配*************/
#define XMGScreenW [UIScreen mainScreen].bounds.size.width
#define XMGScreenH [UIScreen mainScreen].bounds.size.height
#define iphone6P (XMGScreenH == 736)
#define iphone6 (XMGScreenH == 667)
#define iphone5 (XMGScreenH == 568)
#define iphone4 (XMGScreenH == 480)
广告界面控制器:
#import "XMGAdViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "XMGADItem.h"
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#import "XMGTabBarController.h"
@interface XMGAdViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (weak, nonatomic) IBOutlet UIView *adContainView;
@property (nonatomic, weak) UIImageView *adView;
@property (nonatomic, strong) XMGADItem *item;
@property (nonatomic, weak) NSTimer *timer;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
@end
@implementation XMGAdViewController
// 点击跳转做的事情
- (IBAction)clickJump:(id)sender {
// 销毁广告界面,进入主框架界面
XMGTabBarController *tabBarVc = [[XMGTabBarController 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];
}
}
- (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];
}
- (void)timeChange
{
// 倒计时
static int i = 3;
if (i == 0) {
[self clickJump:nil];
}
i--;
// 设置跳转按钮文字
[_jumpBtn setTitle:[NSString stringWithFormat:@"跳转 (%d)",i] forState:UIControlStateNormal];
}
#pragma mark - 加载广告数据
- (void)loadAdData
{
// 1.创建请求会话管理者
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 2.拼接参数
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"code2"] = code2;
// 3.发送请求
[mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
[responseObject writeToFile:@"/Users/xiaomage/Desktop/课堂共享/11大神班上课资料/08-项目/0315/代码/04-广告/ad.plist" atomically:YES];
// 请求数据 -> 解析数据(写成plist文件) -> 设计模型 -> 字典转模型 -> 展示数据
// 获取字典
NSDictionary *adDict = [responseObject[@"ad"] lastObject];
// 字典转模型
_item = [XMGADItem mj_objectWithKeyValues:adDict];
// 创建UIImageView展示图片 =>XMGScreenW:屏幕适配
CGFloat h = XMGScreenW / _item.w * _item.h;
self.adView.frame = CGRectMake(0, 0, XMGScreenW, h);
// 加载广告网页
[self.adView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
// 设置启动图片
- (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"];
}
}
@end
网友评论