1.创建一个广告控制器,成为窗口根控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 进入广告界面
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 创建广告界面:展示启动界面
TGADViewController *adVc = [[TGADViewController alloc] init];
self.window.rootViewController = adVc;
// 3.显示窗口 makeKey:UIApplication主窗口
// 窗口会把根控制器的view添加到窗口
[self.window makeKeyAndVisible];
return YES;
}
UI.png
@interface TGADViewController ()
// 展示广告图片控件
@property (weak, nonatomic) UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
@property (weak, nonatomic) IBOutlet UIImageView *lauchImageView;
@property (weak, nonatomic) IBOutlet UIView *adView;
@property (nonatomic ,strong) TGADItem *item;
@property (nonatomic, weak) NSTimer *timer;
@end
@implementation TGADViewController
#pragma mark -点击跳转按钮
- (IBAction)clickJump:(id)sender {
// 跳转到主框架界面:push,modal,更改窗口的根控制器
UITabBarController *tabBarVc = [[UITabBarController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
// 销毁定时器
[_timer invalidate];
}
#pragma mark - 点击广告图片
- (void)tap {
// 跳转到广告界面
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:[NSURL URLWithString:_item.ori_curl]]) {
[app openURL:[NSURL URLWithString:_item.ori_curl]];
}
}
- (UIImageView *)imageView {
if (_imageView == nil) {
UIImageView *imageView = [[UIImageView alloc] init];
_imageView = imageView;
[self.adView addSubview:imageView];
imageView.userInteractionEnabled = YES;
// 添加点按手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
[imageView addGestureRecognizer:tap];
}
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 设置启动图片 -> 根据不同屏幕 加载不同图片 (屏幕适配)
[self setupLaunchImage];
// 加载广告数据
[self loadData];
// 创建定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
// 每隔1秒就会调用
- (void)timeChange {
static int i = 3;
if (i <= 0) { // 计时完成
[self clickJump:nil];
}
I--;
// 设置按钮标题
NSString *str = [NSString stringWithFormat:@"跳过 (%d)",I];
[self.jumpBtn setTitle:str forState:UIControlStateNormal];
}
// 加载广告数据
- (void)loadData {
// 1.创建请求会话管理者
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 2.创建请求参数:字典
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"code2"] = XMGCode2;
// 3.发送请求(get,post)
[mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
// 解析数据 -> 写成plist文件 -> 字典转模型 -> 模型数据展示界面
// 获取广告字典数据
NSDictionary *adDict = [responseObject[@"ad"] firstObject];
// 字典转模型
_item = [TGADItem mj_objectWithKeyValues:adDict];
// 防止除以0
if (_item.w <= 0) return;
// 展示界面
CGFloat w = XMGScreenW;
CGFloat h = XMGScreenW / _item.w * _item.h;
self.imageView.frame = CGRectMake(0, 0, w, h);
// 加载广告图片
[self.imageView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 请求失败
NSLog(@"%@",error);
}];
}
// 加载启动图片
- (void)setupLaunchImage {
// iphone6: 667 iphone6 5 : 568 iphone6 4 : 480 6P: 736
// 根据不同屏幕高度 加载不同图片
UIImage *image = nil;
if (iphone6P) { // 6P
image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
} else if (iphone6) {
image = [UIImage imageNamed:@"LaunchImage-800-667h"];
} else if (iphone5) {
image = [UIImage imageNamed:@"LaunchImage-700-568h"];
} else if (iphone4) {
image = [UIImage imageNamed:@"LaunchImage-700"];
}
_lauchImageView.image = image;
}
@end
advertisement.png
网友评论