最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS應(yīng)用開(kāi)發(fā)中AFNetworking庫(kù)的常用HTTP操作方法小結(jié)

 更新時(shí)間:2016年05月16日 15:27:15   投稿:goldensun  
AFNetworking庫(kù)是Objective-C語(yǔ)言寫(xiě)成的用于處理HTTP的第三方庫(kù),在GitHub上開(kāi)源并且一直在被更新和維護(hù),下面就一起來(lái)看一下iOS應(yīng)用開(kāi)發(fā)中AFNetworking庫(kù)的常用HTTP操作方法小結(jié)

準(zhǔn)備
首先,你需要將AFNetworking 框架包含到工程中。如果你還沒(méi)有AFNetworking的話,在這里下載最新的版本:
https://github.com/AFNetworking/AFNetworking
當(dāng)你解壓出下載的文件后,你將看到其中有一個(gè)AFNetworking子文件夾,里面全是.h 和 .m 文件, 如下高亮顯示的:

2016516152538410.jpg (238×500)

將AFNetworking拖拽到Xcode工程中.

2016516152554241.jpg (700×474)

當(dāng)出現(xiàn)了添加文件的選項(xiàng)時(shí),確保勾選上Copy items into destination group's folder (if needed) 和 Create groups for any added folders.
將AFNetworking添加到預(yù)編譯頭文件,意味著這個(gè)框架會(huì)被自動(dòng)的添加到工程的所有源代碼文件中。

常用方法介紹
方法一:GET 請(qǐng)求

復(fù)制代碼 代碼如下:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

方法二:POST 請(qǐng)求
復(fù)制代碼 代碼如下:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

方法三:POST Multi-Part Request
復(fù)制代碼 代碼如下:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

方法四:創(chuàng)建一個(gè)下載文件任務(wù)
復(fù)制代碼 代碼如下:

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];


方法五:創(chuàng)建一個(gè)上傳文件任務(wù)
復(fù)制代碼 代碼如下:

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];


方法六:創(chuàng)建一個(gè)上傳文件任務(wù)并顯示進(jìn)度
復(fù)制代碼 代碼如下:

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];
[uploadTask resume];


方法七:創(chuàng)建一個(gè)上傳數(shù)據(jù)data任務(wù)
復(fù)制代碼 代碼如下:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

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

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];
[dataTask resume];


方法八:獲取網(wǎng)絡(luò)狀態(tài)
復(fù)制代碼 代碼如下:

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
}];

方法九: HTTP Manager Reachability
復(fù)制代碼 代碼如下:

NSURL *baseURL = [NSURL URLWithString:@"http://example.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];
            break;
        case AFNetworkReachabilityStatusNotReachable:
        default:
            [operationQueue setSuspended:YES];
            break;
    }
}];

[manager.reachabilityManager startMonitoring];


方法十:AFHTTPRequestOperation的GET請(qǐng)求
復(fù)制代碼 代碼如下:

NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op]; 

方法十一:Batch of Operations
復(fù)制代碼 代碼如下:

NSMutableArray *mutableOperations = [NSMutableArray array];
for (NSURL *fileURL in filesToUpload) {
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [mutableOperations addObject:operation];
}

NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
    NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
    NSLog(@"All operations in batch complete");
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];


方法十二:獲取請(qǐng)求的一些信息(我也沒(méi)有用過(guò),不太常用)
復(fù)制代碼 代碼如下:

Request Serialization

Request serializers create requests from URL strings, encoding parameters as either a query string or HTTP body.

NSString *URLString = @"http://example.com";
NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};
Query String Parameter Encoding

[[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:URLString parameters:parameters error:nil];

GET http://example.com?foo=bar&baz[]=1&baz[]=2&baz[]=3
URL Form Parameter Encoding

[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];

POST http://example.com/
Content-Type: application/x-www-form-urlencoded

foo=bar&baz[]=1&baz[]=2&baz[]=3
JSON Parameter Encoding

[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];

POST http://example.com/
Content-Type: application/json

{"foo": "bar", "baz": [1,2,3]}

相關(guān)文章

  • iOS中關(guān)于UIWindow和statusbar的設(shè)置問(wèn)題

    iOS中關(guān)于UIWindow和statusbar的設(shè)置問(wèn)題

    最近在做開(kāi)發(fā)時(shí)要做一個(gè)類似于UIAlertView的控件,做法是創(chuàng)建一個(gè)基于UIView的類,在里面進(jìn)行自定義控件的設(shè)置,為了盡量模仿UIAlertView,在這個(gè)類里面創(chuàng)建了一個(gè)新的UIWindow并將self顯示到這個(gè)window上
    2017-03-03
  • iOS第三方框架二維碼生成與掃描

    iOS第三方框架二維碼生成與掃描

    這篇文章主要為大家詳細(xì)介紹了iOS第三方框架二維碼生成與掃描,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • iOS獲取到用戶當(dāng)前位置

    iOS獲取到用戶當(dāng)前位置

    這篇文章主要為大家詳細(xì)介紹了iOS獲取到用戶當(dāng)前位置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 詳解IOS11新特性之larget title的實(shí)現(xiàn)

    詳解IOS11新特性之larget title的實(shí)現(xiàn)

    本篇文章主要介紹了詳解IOS11新特性之larget title的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • iOS設(shè)置圓角的三種方式

    iOS設(shè)置圓角的三種方式

    本文給大家分享ios設(shè)置圓角的三種方式,相對(duì)來(lái)說(shuō)最簡(jiǎn)單的一種是第一種方法,具體內(nèi)容詳情參考下本文
    2017-03-03
  • iOS中NSInvocation的基本用法教程

    iOS中NSInvocation的基本用法教程

    NSInvocation是IOS消息傳遞和方法調(diào)用的一個(gè)類,下面這篇文章主要給大家介紹了關(guān)于iOS中NSInvocation的基本用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面隨著小編來(lái)一起看看吧。
    2017-09-09
  • iOS 四種回調(diào)方法總結(jié)

    iOS 四種回調(diào)方法總結(jié)

    這篇文章主要介紹了iOS 四種回調(diào)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 代碼詳解iOS視頻直播彈幕功能

    代碼詳解iOS視頻直播彈幕功能

    本篇文章通過(guò)原理分析和代碼實(shí)例講述了實(shí)現(xiàn)iOS視頻直播彈幕功能的方法,需要的朋友參考學(xué)習(xí)下吧。
    2017-12-12
  • Objective-C中NSArray的基本用法示例

    Objective-C中NSArray的基本用法示例

    這篇文章主要介紹了Objective-C中NSArray的基本用法示例,包括基本的排序等方法的介紹,需要的朋友可以參考下
    2015-09-09
  • 關(guān)于iOS導(dǎo)航欄返回按鈕問(wèn)題的解決方法

    關(guān)于iOS導(dǎo)航欄返回按鈕問(wèn)題的解決方法

    這篇文章主要為大家詳細(xì)介紹了關(guān)于iOS導(dǎo)航欄返回按鈕問(wèn)題的解決方法,對(duì)iOS自定義backBarButtonItem的點(diǎn)擊事件進(jìn)行介紹,感興趣的小伙伴們可以參考一下
    2016-05-05

最新評(píng)論

安泽县| 三江| 射洪县| 昌黎县| 南阳市| 且末县| 平阴县| 和硕县| 松潘县| 海晏县| 察隅县| 三亚市| 银川市| 饶河县| 项城市| 江西省| 绥芬河市| 临洮县| 云和县| 盖州市| 恩平市| 丹凤县| 新龙县| 仙居县| 延寿县| 阿荣旗| 曲靖市| 尤溪县| 含山县| 盖州市| 靖边县| 辽宁省| 沽源县| 东阳市| 鲜城| 乐昌市| 永新县| 墨竹工卡县| 汶川县| 石屏县| 仙游县|