AFNnetworking 빠 른 튜 토리 얼, 공식 입문 튜 토리 얼 번역

AFNetworking 홈 페이지 입문 강좌
AFNetworking 은 빠 른 속도 로 사용 할 수 있 는 ios 와 mac os x 의 네트워크 프레임 워 크 입 니 다. Foundation URL Loading System 위 에 구 축 된 것 으로 네트워크 의 추상 적 인 층 을 봉 하여 편리 하 게 사용 할 수 있 습 니 다. AFNetworking 은 모듈 화 된 구조 로 풍부 한 api 프레임 워 크 를 가지 고 있 습 니 다.
1. HTTP 요청 과 작업: 1. AFHTTPRequestOperationManager: 이러한 패 키 징 은 웹 응용 프로그램 과 통신 하여 HTTP 를 통 해 제작 요구, 직렬 화 응답, 네트워크 접근 성 모니터링 과 안전성, 그리고 경영 관 리 를 요구 하 는 흔 한 모델 을 포함한다.
GET 요청:
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 폼 인자 가 있 는 POST 요청:
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 형식의 폼 파일 업로드 요청:
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);
}];

2. Session 관리: 1. AFURLSessionManager: 제 정 된 NSURLSession 대상 2, NSURLSessionConfiguration 대상 은 반드시 < NSURLSessionTaskDelegate >, < NSURLSessionDataDelegate >, < NSURLSessionDownloadDelegate >, < NSURLSessionDelegate > 프로 토 콜 을 실현 해 야 합 니 다.
다운로드 작업 만 들 기:
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];

데이터 흐름 작업 만 들 기:
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];

4. AFHTTPRequestOperation 1, AFHTTPRequestOperation 을 사용 하 는 것 은 HTTP 또는 HTTPS 프로 토 콜 을 사용 하 는 AFURLConnection Operation 의 하위 클래스 입 니 다.
봉 인 된 HTTP 상태 와 형식 은 요청 의 성공 여 부 를 결정 합 니 다.
2. AFHTTPRequestOperation Manager 는 일반적으로 가장 좋 은 요청 방식 이지 만 AFHTTPRequestOpersion 도 단독으로 사용 할 수 있 습 니 다.
GET 방식 으로:
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];

연속 동작 여러 개:
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];


AFNetworking 홈 페이지:
http://afnetworking.com/
문서 원본:http://cocoadocs.org/docsets/AFNetworking/2.0.3/

좋은 웹페이지 즐겨찾기