標籤:字串 工程 監控 介面 網路
一 下載: 網址 -- github
二 環境:
需要引入的庫 - CoreLocation.framework
SystemConfiguration.framework
MobileCoreServices.framework
Security.framework
需要在 ARC 的環境下 - 非 ARC 的工程中 - 請添加 -fobjc-arc
三 結構:
1 : AFHTTPClient -- 提供了一個方便的網路互動介面,包括預設頭,身分識別驗證,是否串連到網路,批量處理操作,查詢字串參數序列化,已經多種表單請求
2 : AFHTTPRequestOperation --
和它得子類可以基於http狀態和內容列下來區分是否成功請求了
3 : AFURLConnectionOperation --
和它的子類繼承NSOperation的,允許請求被取消,暫停/恢複和由NSOperationQueue進行管理。
4 : AFURLConnectionOperation --
可以讓你輕鬆得完成上傳和下載,處理驗證,監控上傳和下載進度,控制的緩衝。
四 使用 :
用法情境 1 : 請求api資料
方法1 --
建立一個 AFHTTPClient 執行個體
AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.jiepang.com"]];
// 這裡一定要有一個 baseURL - 不然會拋出一個異常 --
建立一個 NSURLRequest 執行個體
NSDictionary * params = @{@"apiver": @"4",@"id" : @"830755858",@"extra_info": @"1"};
NSURLRequest * request = [client requestWithMethod:@"POST"
path:@"users/show"
parameters:params];
@param method 網路請求方式,比如 GET,POST,PUT,DELETE , 不能為 nil --
@param path 和 baseURL 組合成為一個 url -- 通俗的說,就是我們調用的介面 -- 比如,我想調用http://api.jiepang.com/users/show,這個介面 -- 那麼 baseURL 為 http://api.jiepang.com -- path 為users/show
@param parameters 網路請求參數 -- http://api.jiepang.com/v1/users/show?id=123 -- id=123 就是這個參數
建立一個 AFHTTPRequestOperation 執行個體進行網路連結
AFHTTPRequestOperation * operation = [client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success obj == %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"faild , error == %@ ", error);
}];
[operation start];
這樣 - 一次簡單的調用就OK了 -
當然也可以,用封裝好的其它方法 --
- (void)getPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
- (void)deletePath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
- (void)patchPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
省去了組裝 Resquest 的時間 --
比如 -
方法二 --
[client getPath:path
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString * obj = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"obj == %@",obj);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"faild -- ");
}];
這和上面的效果是一樣的 --
我們看到,前面返回的資料全部為 NSdata - 為我們添加了麻煩 -- 而且我們大部分的 api 返回資料都為 json --
同樣 - 我們可以用 AFHTTPRequestOperation 的子類 AFJSONRequestOperation 來替代 --
方法三 --
NSDictionary * params = @{@"apiver": @"4",@"id" : @"830755858",@"extra_info": @"1"};
AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.jiepang.com"]];
NSURLRequest * request = [client requestWithMethod:@"POST"
path:@"users/show"
parameters:params];
AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"json == %@",JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"faild -- ");
}];
[operation start];
使用情境 2 : 非同步載入圖片
AFImageRequestOperation 是繼承自 AFHTTPRequestOperation -- 所以 - 方法大同小異 --
NSString * url = @"http://c.hiphotos.baidu.com/album/w=2048/sign=1d7ca85bac345982c58ae29238cc30ad/f2deb48f8c5494ee7abe33362cf5e0fe99257e04.jpg";
// 這是一個大美女
建立 request
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
AFImageRequestOperation * operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:^UIImage *(UIImage *image) {
UIImage * tempImage = [UIImage imageWithCGImage:
CGImageCreateWithImageInRect(image.CGImage,ake(0, 0, image.size.width, image.size.height/2.0))];
return tempImage;
} success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"reload image success ");
_imageView.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"reload image faild , error == %@ ",error);
}];
[operation start];
這個方法裡有三個block ,success 和 failure 不說 -- processimageBlock -- 是在 圖片載入完後,對 圖片 處理的 block ,可以為 nil ,在 success 之前調用 --
iOS 使用AFNetworking