需求:在啟動的時候诗赌,一般會去先加載廣告界面续搀,需要一個自定義類去管理這個廣告界面
分析:
(1) 若使用系統(tǒng)本身的LaunchScreen.storyboard
个榕,就無法使用自定義的類
(2) 廣告業(yè)務(wù)邏輯
(3) 占位視圖思想:有個控件不確定尺寸,但是層次結(jié)構(gòu)已經(jīng)確定,就可以使用占位視圖思想
(4) 屏幕適配.通過屏幕高度判斷
報錯: unacceptable content-type: text/html"
原因: 響應(yīng)頭問題,AFN發(fā)送網(wǎng)絡(luò)請求,返回的數(shù)據(jù)是 text/html類型扛拨,AFN不能夠接收
解決:manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
拓展:為什么使用static來修飾int
原因:因為這是全局的棧猫缭,只分配一次內(nèi)存葱弟,若不用static修飾,會每次調(diào)用的時候就分配一次內(nèi)存猜丹。
方案解析:設(shè)置窗口的根控制器為廣告控制器
(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
// 點擊跳轉(zhuǎn)做的事情
- (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;
}
// 點擊廣告界面調(diào)用(點擊廣告射窒,打開相應(yīng)的網(wǎng)頁)
- (void)tap
{
// 跳轉(zhuǎn)到界面 => safari
NSURL *url = [NSURL URLWithString:_item.ori_curl];
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:url]) {
[app openURL:url];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// 設(shè)置啟動圖片
[self setupLaunchImage];
// 加載廣告數(shù)據(jù) => 拿到活時間 => 服務(wù)器 => 查看接口文檔 1.判斷接口對不對 2.解析數(shù)據(jù)(w_picurl,ori_curl:跳轉(zhuǎn)到廣告界面,w,h) => 請求數(shù)據(jù)(AFN)
[self loadAdData];
// 創(chuàng)建定時器
_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--;
// 設(shè)置跳轉(zhuǎn)按鈕文字
[_jumpBtn setTitle:[NSString stringWithFormat:@"跳轉(zhuǎn) (%d)",i] forState:UIControlStateNormal];
}
#pragma mark - 加載廣告數(shù)據(jù)
- (void)loadAdData
{
// 1.創(chuàng)建請求會話管理者
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 2.拼接參數(shù)
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"code2"] = code2;
// 3.發(fā)送請求
[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];
// 請求數(shù)據(jù) -> 解析數(shù)據(jù)(寫成plist文件) -> 設(shè)計模型 -> 字典轉(zhuǎn)模型 -> 展示數(shù)據(jù)
// 獲取字典
NSDictionary *adDict = [responseObject[@"ad"] lastObject];
// 字典轉(zhuǎn)模型
_item = [XMGADItem mj_objectWithKeyValues:adDict];
// 創(chuàng)建UIImageView展示圖片 =>XMGScreenW:屏幕適配
CGFloat h = XMGScreenW / _item.w * _item.h;
self.adView.frame = CGRectMake(0, 0, XMGScreenW, h);
// 加載廣告網(wǎng)頁
[self.adView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
// 設(shè)置啟動圖片
- (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