美文网首页ios开发整理
应用添加广告启动页

应用添加广告启动页

作者: 冲浪小子 | 来源:发表于2017-07-21 11:42 被阅读0次

通常一个app启动的时候会加载一个广告启动页。如下图:


ADStartPage.gif

不啰嗦直接上代码

  • 首先创建QBYADViewController类继承UIViewController并且勾选创建xib文件。因为界面比较简单所以用xib描述。
    界面如下图:
xib.png

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

// [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

相关文章

  • 应用添加广告启动页

    通常一个app启动的时候会加载一个广告启动页。如下图: 不啰嗦直接上代码 首先创建QBYADViewControl...

  • iOS 启动页广告详解

    现在的项目中, 添加广告越来越普遍, 那么在启动页添加广告就是重中之重, 下面是我开发过程中的经验. 添加启动页广...

  • ios 启动添加广告页

    由于ios启动页是有苹果的规范的,所以动态的修改启动页可能会有被拒的风险。关于这一点我没试过,如果想做是可以做的,...

  • 启动launchOptions

    给启动页加上一页广告页,在收到通知跳应用是,不走启动页的rootvc设置方法。 做的判断是if(launchOpt...

  • 关于启动页添加广告的一些想法

    闲来无事,封装了一个广告页,还算比较实用,首先我们要知道如何来添加这个广告,以及添加在哪里,启动页上都有什么,逻辑...

  • 金蝶云苍穹如何配置应用首页 ?

    设置应用菜单 新建分组 新建组里对应的页面 在应用管理页添加应用 PS:在添加应用前必须先启动应用并给应用分配权限...

  • Android实践 | 启动页 Splash Screen 实现

    现在很多应用都会在进入主界面之前,添加一个启动页,然后加入几秒钟的广告,我觉得这个不能算是 “真正意义上的 “ 启...

  • 拦截各种应用启动页广告

    ◈小鸣闲话 ◈推荐正文 ◈资源获取与安装 END

  • 启动页

    启动页的使用场景 1.被动添加 因为应用启动需要耗费一些时间,为了给用户合理的反馈 2.主动添加 (1).用户下载...

  • XL面试题总结-iOS

    1. 基础的sql语句 2. 应用运行启动如何加入广告页面 封装一个广告页,包括广告跳转和倒计时跳过功能。didF...

网友评论

    本文标题:应用添加广告启动页

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