如何检测NSURLConnection sendAsynchronousRequest是否完成

时间:2013-04-26 21:29:05

标签: iphone ios ipad nsurlconnection nsurlconnectiondelegate

我正在使用sendAsynchronousRequest:queue:completionHandler:上传一个文件(我已经删除了一些旧的第三方库,转而使用这种原生方法进行直接调用。我已经按原样保留了NSURLRequest和字典。)。我无法弄清楚如何告诉它如何完成推送文件。我认为在completionHandler中会定期调用,我们可以检查传入的参数,但它们似乎不包含我需要的东西。

- (void)sendToServer:(NSString*)url asset:(MYAsset *)asset path:(NSString *)file completion:(MYUploaderBoolBlock)completion{
    NSDictionary *uploadParameters = asset.s3Info;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:uploadParameters[@"my_url"]]];
    request.HTTPMethod = @"PUT";
    request.timeoutInterval = 300;
    [request addValue:uploadParameters[@"date"] forHTTPHeaderField:@"Date"];
    [request addValue:uploadParameters[@"authorization"] forHTTPHeaderField:@"Authorization"];
    [request addValue:uploadParameters[@"content_type"] forHTTPHeaderField:@"Content-Type"];
    [request addValue:asset.md5 forHTTPHeaderField:@"Content-MD5"];
    [request addValue:@"public-read" forHTTPHeaderField:@"x-amz-acl"];
    [request addValue:[@(asset.sizeInKB) stringValue] forHTTPHeaderField:@"Content-Length"];
    request.HTTPBodyStream = [NSInputStream inputStreamWithFileAtPath:file];

    DDLogInfo(@"Uploading %@ to server", uploadParameters[@"my_url"]);
    DDLogInfo(@"HTTP Headers: %@", [request allHTTPHeaderFields]);

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:self.queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if ([data length] > 0 && error == nil){
                                   // I am expecting that this can be used to detect progress of the file upload
                                   // as well as completion?
                                   NSLog(@"received data %d/%d =%f%", data.length, response.expectedContentLength, data.length/(float)response.expectedContentLength);
                                   if(data.length == response.expectedContentLength){
// This never fires because expectedContentLength is always -1
                                       completion(YES);
                                   }
                               }
                               else if ([data length] == 0 && error == nil){
                                   NSLog(@"reply empty");
                               }
                               else if (error != nil && error.code == -1001){
                                   NSLog(@"timed out");
                               }
                               else if (error != nil){
                                   NSLog(@"Error %@", error.localizedDescription);
                                   completion(NO);
                               }
                           }];

}

2 个答案:

答案 0 :(得分:3)

根据documentation,在异步发送结束时调用一次完成处理程序块 如果error为nil,则操作成功(不确定为什么要测试数据长度)。

答案 1 :(得分:0)