// XMGAdViewController.m #import "XMGAdViewController.h" #import <AFNetworking/AFNetworking.h> #import "XMGADItem.h" #import <MJExtension/MJExtension.h> #import <UIImageView+WebCache.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; @end @implementation XMGAdViewController - (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]; // Do any additional setup after loading the view. // 设置启动图片 [self setupLaunchImage]; // 加载广告数据 => 拿到活时间 => 服务器 => 查看接口文档 1.判断接口对不对 2.解析数据(w_picurl,ori_curl:跳转到广告界面,w,h) => 请求数据(AFN) [self loadAdData]; } /* 1.以后添加东西.首先想到加多少次 */ #pragma mark - 加载广告数据 - (void)loadAdData { // unacceptable content-type: text/html" 响应头 // 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展示图片 => 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