1疏哗、使用AFNetwoking
//導入
#import "AFNetworking.h"
#import "UIKit+AFNetworking.h"
//和導入系統(tǒng)框架 MobileCoreServices堂竟、SystemConfiguration蟋恬、sec 咽安。突雪。忽略
網(wǎng)絡(luò)監(jiān)聽
AFNetworkReachabilityManager 網(wǎng)絡(luò)監(jiān)聽管理者對象
方法:
設(shè)置監(jiān)聽
setReachabilityStatusChangeBlock
啟動監(jiān)聽
startMonitoring
代碼:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//創(chuàng)建網(wǎng)絡(luò)監(jiān)聽管理者對象 AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
/* typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
AFNetworkReachabilityStatusUnknown = -1,//未識別的網(wǎng)絡(luò)
AFNetworkReachabilityStatusNotReachable = 0,//不可達的網(wǎng)絡(luò)(未連接)
AFNetworkReachabilityStatusReachableViaWWAN = 1,//2G,3G,4G...
AFNetworkReachabilityStatusReachableViaWiFi = 2,//wifi網(wǎng)絡(luò) }; */
//設(shè)置監(jiān)聽
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未識別的網(wǎng)絡(luò)");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"不可達的網(wǎng)絡(luò)(未連接)");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"2G,3G,4G...的網(wǎng)絡(luò)");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"wifi的網(wǎng)絡(luò)");
break;
default:
break;
}
}];
//開始監(jiān)聽
[manager startMonitoring];}
下載
//
NSURLSessionConfiguration
> + defaultSessionConfiguration
:創(chuàng)建一個默認的NSURLSessionConfiguration
AFURLSessionManager
> - initWithSessionConfiguration
:初始化AFURLSessionManager 通過 NSURLSessionConfiguration
> - downloadTaskWithRequest
:創(chuàng)建 NSURLSessionDownloadTask 對象
NSURLSessionDownloadTask
> - resume
代碼:
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
//繼承自AFURLSessionManager。類似于1和2的關(guān)系颠猴。也是方便HTTP以及https請求的使用,增加了一些接口括袒,方便調(diào)用。
AFURLSessionManager *sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfig];
NSURL *url = [NSURL URLWithString:@"http://www.361810.com/uploads/allimg/150409/2053363O4-0.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//progress 進度條 destination 最終地址 completionHandler 完成處理程序
NSURLSessionDownloadTask *downloadTask = [sessionManager downloadTaskWithRequest:request progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]);
//創(chuàng)建下載地址
NSURL *downloadURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [downloadURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//這時候已經(jīng)在主線程了
//先將filePath轉(zhuǎn)換成data類型
NSData *data = [NSData dataWithContentsOfURL:filePath];
//轉(zhuǎn)換成image
UIImage *image = [UIImage imageWithData:data];
_imageView.image = image;
}];
//繼續(xù)操作
[downloadTask resume];
上傳
get post 解析