美文网首页
06-广告界面

06-广告界面

作者: 小胖子2号 | 来源:发表于2017-05-10 16:13 被阅读27次

需求:在启动的时候,一般会去先加载广告界面,需要一个自定义类去管理这个广告界面

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

相关文章

  • 06-广告界面

    需求:在启动的时候,一般会去先加载广告界面,需要一个自定义类去管理这个广告界面 分析:(1) 若使用系统本身的La...

  • 广告界面

    一.广告界面业务逻辑 1.什么时候进入广告界面(程序启动完成的时候,进入广告界面) 为什么?程序启动的时候,我们没...

  • 开始停用简书

    打开界面有广告,体验卡顿

  • iOS逆向-网易云音乐篇

    照旧去点广告 效果图在下面 1.启动页广告启动页广告 2.歌曲播放界面导航栏右边直播弹框(直接在设置界面有开关)直...

  • 杂⑦碎八之--百思Demo Z3

    广告界面 1.业务逻辑:程序启动完成之后,进入广告界面.因为程序启动的时候,没有办法处理,苹果没有提供对应的API...

  • ios 入屏广告

    主界面上蒙版广告。//背景_retuAnimview = [[ReturnAnimationView alloc]...

  • 迅雷x10.1.8.284去广告安装纯净版下载

    软件:迅雷x 去广告安装版 下载地址 界面:

  • 从今天起

    从今天起 卸掉满是广告 与花边新闻的手机浏览器 要用就下载一个 夸克 吧 界面干干净净 界面没有广告和新闻干扰 不...

  • 计算广告笔记06-程序化交易广告

    RTB的产生使得广告市场向着开放的竞价平台的方向发展,这样的平台就是广告交易平台,ADX,其主要特征是用RTB的方...

  • 手机屏蔽广告软件Adguard

    Adguard,是一款很不错的屏蔽手机广告软件,包括软件打开界面的广告啊,Q邮箱广告邮件,百度搜索的广告啊都会被屏...

网友评论

      本文标题:06-广告界面

      本文链接:https://www.haomeiwen.com/subject/xyjitxtx.html