06-廣告界面

需求:在啟動的時候,一般會去先加載廣告界面,需要一個自定義類去管理這個廣告界面

分析:
(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容