iOS常用網(wǎng)絡(luò)請求
1.NSURLConnection
2.NSURLSession
3.AFNetworking
NSURLConnection在iOS9被宣布棄用,
NSURLSession是蘋果在iOS7后為HTTP數(shù)據(jù)傳輸提供的一系列接口,
AFNetworking是一個為 iOS 和 Mac OSX 制作的令人愉快的網(wǎng)絡(luò)庫冯吓。
當前使用 AFNetworking 3.0 版本
AFNetworking 下載地址 https://github.com/AFNetworking/AFNetworking
NSURLConnection
get異步請求
// 請求路徑
NSString *urlString = @"http://www.weather.com.cn/data/sk/101010100.html?city=nanjing";
// 創(chuàng)建URL
NSURL *url = [NSURL URLWithString:urlString];
// 創(chuàng)建請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 設(shè)置請求方法(默認就是GET請求,可以不設(shè)置)
request.HTTPMethod = @"GET";
// 發(fā)送異步請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
}];
post異步請求
// 請求路徑
NSString *urlString = @"http://www.weather.com.cn/data/sk/101010100.html";
// 創(chuàng)建URL
NSURL *url = [NSURL URLWithString:urlString];
// 創(chuàng)建請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 設(shè)置請求方法
request.HTTPMethod = @"POST";
// 設(shè)置請求體
request.HTTPBody = [@"city=南京&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
// 發(fā)送異步請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
}];
NSURLSession
get 請求
// 快捷方式獲得session對象
// block 在子線程
// NSURLSession *session = [NSURLSession sharedSession];
// block 在主線程
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:@"http://www.weather.com.cn/data/sk/101010100.html?username=daka&pwd=123"];
// 通過URL初始化task,在block內(nèi)部可以直接對返回的數(shù)據(jù)進行處理
NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError* error) {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
}];
// 啟動任務(wù)
[task resume];
post 請求
NSURL *url = [NSURL URLWithString:@"http://www.weather.com.cn/data/sk/101010100.html"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=daka&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
NSURLSession *session = [NSURLSession sharedSession];
// 由于要先對request先行處理,我們通過request初始化task
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
}];
[task resume];
AFNetworking
AFNetworking 2.0以上兼容 NSURLSession
AFNetworking 3.0 get 請求
NSString * urlString = @"http://www.weather.com.cn/data/sk/101010100.html";
NSDictionary *parameters = @{@"username":@"daka",
@"pwd":@"123"};
//1浪慌、創(chuàng)建管理者對象
AFHTTPSessionManager * manager = [NetWorkManager shareSessionManager];
[manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary * responseDict = responseObject;
NSLog(@"%@",responseDict);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
AFNetworking 3.0 post 請求
NSString * urlString = @"http://www.weather.com.cn/data/sk/101010100.html";
NSDictionary *parameters = @{@"username":@"daka",
@"pwd":@"123"};
//1、創(chuàng)建管理者對象
AFHTTPSessionManager * manager = [NetWorkManager shareSessionManager];
[manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary * responseDict = responseObject;
NSLog(@"%@",responseDict);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];