本人是初學(xué)iOS菜鳥一個(gè),對于AFNetworing的框架很是記不住,在此總結(jié)一下,防止以后忘記。
AFNetWorking的官網(wǎng)地址:https://github.com/AFNetworking/AFNetworking
// 1.需要加入SystemConfiguration.framework、MobileCoreServices.framework、Security.framework三個(gè)框架
// 2.消除警告在pch文件中添加#import #import
// 3.引入AFNetworking框架#import "AFNetworking.h"
// 4.下面是詳細(xì)代碼,包括:簡單網(wǎng)絡(luò)請求、圖片請求、JSON數(shù)據(jù)請求、XML數(shù)據(jù)請求、流媒體播放功能
Get請求:
//普通網(wǎng)絡(luò)請求
NSURL*url = [NSURLURLWithString:@"http://192.168.198.125/admin/index.php?m=interface&a=companies"];
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
//準(zhǔn)備AFHTTPRequestOperation
AFHTTPRequestOperation*operation = [[AFHTTPRequestOperationalloc]initWithRequest:request];
[operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSArray*array = operation.responseString;
NSLog(@"%@", array);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
NSLog(@"Failure: %@", [errorlocalizedDescription]);
}];
//開始獲取數(shù)據(jù)
[operationstart];
XML數(shù)據(jù)網(wǎng)絡(luò)請求
NSURLRequest*request2 = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792@N01&format=rest"]];
//準(zhǔn)備AFXMLRequestOperation
AFXMLRequestOperation*operation2 = [AFXMLRequestOperationXMLParserRequestOperationWithRequest:request2success:^(NSURLRequest*request,NSHTTPURLResponse*response,NSXMLParser*XMLParser) {
//獲取成功
XMLParser.delegate=self;
[XMLParserparse];
}failure:^(NSURLRequest*request,NSHTTPURLResponse*response,NSError*error,NSXMLParser*XMLParser) {
//獲取失敗
}];
//開始獲取數(shù)據(jù)
[operation2start];
流媒體數(shù)組獲取
//準(zhǔn)備NSURLRequest
NSURLRequest*request4 = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://localhost:8080/encode"]];
//準(zhǔn)備AFHTTPRequestOperation
AFHTTPRequestOperation*operation4 = [[AFHTTPRequestOperationalloc]initWithRequest:request4];
//設(shè)置inputStrem
operation4.inputStream= [NSInputStreaminputStreamWithFileAtPath:[[NSBundlemainBundle]pathForResource:@"large-image"ofType:@"tiff"]];
//設(shè)置outputStrem
operation4.outputStream= [NSOutputStreamoutputStreamToMemory];
[operation4start];
添加上傳圖片代碼
//將圖片轉(zhuǎn)為NSData形式
NSData*imageData =UIImagePNGRepresentation(_thumbnailView.image);
//要上傳的地址
NSURL*url = [NSURLURLWithString:[NSStringstringWithFormat:@"http://192.168.198.125/index.php?m=Interface&a=aptitude"]];
//初始化AFHttpClient對象
AFHTTPClient*client = [[AFHTTPClientalloc]initWithBaseURL:url];
//初始化NSURLRequest對象
NSURLRequest*request = [clientmultipartFormRequestWithMethod:@"POST"http://發(fā)送請求的方式,需設(shè)置為POST方式
path:Nil//路徑,可為空
parameters:@{
@"tel":@"13900000000",
@"tenantid":kServerPort,
@"aptitude":_aptitudeNameText.text
}//參數(shù)設(shè)置
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//設(shè)置上圖片的數(shù)據(jù)
[formDataappendPartWithFileData:imageData
name:@"aptitude_logo"http://圖片參數(shù)的名稱
fileName:@"aptitude_logo.png"http://上傳的圖片名稱,可與參數(shù)名稱不一致
mimeType:@"image/png"];//圖片類型
}];
//初始化AFHTTPRequestOperation對象
AFHTTPRequestOperation*operation = [[AFHTTPRequestOperationalloc]initWithRequest:request];
//處理上傳數(shù)據(jù)的結(jié)果的代碼塊
[operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
//上傳成功,接收返回來的結(jié)果
NSLog(@"%@", [[NSStringalloc]initWithData:responseObjectencoding:NSUTF8StringEncoding]);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
//上傳失敗,打印錯(cuò)誤信息
NSLog(@"錯(cuò)誤信息:%@", [errorlocalizedDescription]);
}];
//開始上傳
[client.operationQueueaddOperation:operation];
注:你開發(fā)應(yīng)用的時(shí)候,會(huì)經(jīng)常使用到很多第三方開源類庫,當(dāng)類庫更新的時(shí)候,你又必須重新下載然后手工導(dǎo)入是比較麻煩的,所以在這里給大家介紹一個(gè)新的類庫管理工具CocoPods。當(dāng)?shù)谌筋悗煊懈碌臅r(shí)候在CocoPods中只需要一句命令就可解決上面的麻煩,當(dāng)然最重要的一點(diǎn)就是你要正確的設(shè)置CocoPods(絕大多數(shù)有名得開源的類庫都支持CocoPods),所以做開發(fā)經(jīng)常為第三方類庫更新發(fā)愁的騷年們,掌握CocoPods勢在必行!!!
CocoPods教程附上:http://code4app.com/article/cocoapods-install-usage