2016-11-22 63 views
1

我試圖將此代碼遷移到AFNetworking 3.1,但我在HTTPRequestOperationWithRequest函數中遇到了一些問題。從我可以告訴它已被棄用,但我不知道該用什麼來代替。從AFNetworking 2.5.4遷移到3.1

下面是代碼:

- (AFHTTPRequestOperation *)GET:(NSString *)URLString 
        parameters:(NSDictionary *)parameters 
       timeoutInterval:(NSTimeInterval)timeoutInterval 
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success 
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure 
{ 
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil]; 
    [request setTimeoutInterval:timeoutInterval]; 
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; 
    [self.operationQueue addOperation:operation]; 

    return operation; 
} 
+0

訪問... http://stackoverflow.com/questions/34561215/afnetworking-3-0-migration-how-to-post-with-headers-and-http-body/36299737#36299737 – Vvk

回答

1

正如你可以在README AFNetworking看到已切換到NSURLSession。這意味着,去創造你做這樣的事情的請求:

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

你的確切功能是無法複製的,但你也許可以以你的方式回到那個函數的調用,並與類似的東西取代它。