http://www.mamicode.com/info-detail-477899.html
AFNetworking 2.5.0版本的使用
http://cocoadocs.org/docsets/AFNetworking/2.5.0/
1. 下载源码并进行编译
源码地址 http://pan.baidu.com/s/1jG24w3W
2. 判断当前是否有网络,基准网址为 http://baidu.com/
源码:
可以用来监测WWAN或者WiFi或者断网状态;)
// 基准baseURL
NSURL *baseURL = [NSURL URLWithString:@"http://baidu.com/"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO]; // 此处可以发送有网络通知
NSLog(@"有网络"); break;
case AFNetworkReachabilityStatusNotReachable:
default:
[operationQueue setSuspended:YES]; // 此处可以发送断网通知
NSLog(@"断网了"); break;
}
}]; // 开始监测网络
[manager.reachabilityManager startMonitoring];
3. 不带参数的GET请求
源码:
// 初始化网址
NSString *requestAddress = @"http://api.openweathermap.org/data/2.5/forecast/daily?lat=39.88293652833437&lon=116.4621119300779&lang=zh_cn"; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 不带参数的GET请求
[manager GET:requestAddress
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];