AFNetworking

https://github.com/AFNetworking/AFNetworking

AFNetworking 是一款备受喜爱的iOS端和Mac OS X端的网络库。它构建于 Foundation URL Loading System之上, 对cocoa的网络层做了扩展。它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来非常轻松。

Swift可以使用Alamofire

安装

使用CocoaPods安装

Podfile里面加入

1
2
3
4
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

pod 'AFNetworking', '~> 3.0'

使用Carthage安装

Cartfile中加入:

1
github "AFNetworking/AFNetworking" ~> 3.0

3.0版本只支持iOS 7OS X 10.9以上的版本

使用

导入头文件

1
#import <AFNetworking.h>

检查网络是否连接

1
2
3
4
5
6
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));

}];

[[AFNetworkReachabilityManager sharedManager] startMonitoring];

其中status表示网络状态,通过Wi-Fi链接返回2,用手机流量返回1,无连接为0,为止状态为-1 。

1
2
3
4
5
6
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
};

请求JSON数据

1
2
3
4
5
6
7
8
9
10
11
12
13
NSURL *URL = [NSURL URLWithString:@"http://example.com"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);


//如果穿回来的数据带有数组可以这样
//NSArray *data = [responseObject objectForKey:@"data"];
//NSString *name = [data[0] objectForKey:@"name"];
//NSLog(@"name: %@", name);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

其中parameters可以用NSDictionary,如

1
NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};

post数据的话将 GET改为POST就可以了~


下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];

上传文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];

从2.0迁移到3.0的方法,官方文档