IOS开发基础之网易新闻环境搭建异步请求json,AFN网络封装第1天
视频资料是2015年的,但是AFN是导入框架的关键文件,我尝试使用cocoapods安装最新的AFN,虽然成功了,但是版本太高,导致API函数有所不同,先学习过期的,再学习现在的。源码在我的主页下。
// HMNetworkTools.h
// 01-网易新闻搭建
// Created by 鲁军 on 2021/4/11.
#import <Foundation/Foundation.h>
#import "AFHTTPSessionManager.h"
@interface HMNetworkTools : AFHTTPSessionManager
+(instancetype) sharedManager;
@end
//
// HMNetworkTools.m
// 01-网易新闻搭建
//
// Created by 鲁军 on 2021/4/11.
//
#import "HMNetworkTools.h"
@implementation HMNetworkTools
+ (instancetype)sharedManager{
static id ins = nil;
static dispatch_once_t onceToken;
//http://c.m.163.com/nc/ad/headline/0-4.html
NSURL *baseURL = [NSURL URLWithString:@"http://c.m.163.com/nc/"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.timeoutIntervalForRequest = 15;
dispatch_once(&onceToken, ^{
ins = [[self alloc] initWithBaseURL:baseURL sessionConfiguration:config];
});
return ins;
}
@end
//
// ViewController.m
// 01-网易新闻搭建
//
// Created by 鲁军 on 2021/4/11.
#import "ViewController.h"
#import "HMNetworkTools.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[HMNetworkTools sharedManager] GET:@"ad/headline/0-4.html" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
@end