Iphone 如何检测NSURLConnection sendAsynchronousRequest是否已完成

Iphone 如何检测NSURLConnection sendAsynchronousRequest是否已完成,iphone,ios,ipad,nsurlconnection,nsurlconnectiondelegate,Iphone,Ios,Ipad,Nsurlconnection,Nsurlconnectiondelegate,我正在使用sendAsynchronousRequest:queue:completionHandler:上载一个文件(我已经删除了一些旧的第三方库,以支持使用此本机方法进行直接调用。我保留了NSURLRequest和字典)。我不知道如何告诉它是如何完成推送文件的。我想在completionHandler中,它将定期调用,并且我们可以检查传入的参数,但是它们似乎不包含我需要的内容 - (void)sendToServer:(NSString*)url asset:(MYAsset *)asset

我正在使用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);
                               }
                           }];

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

  • 那么使用它呢。。然后使用它的

  • 或者使用框架


啊,我错了。我忽略了其中的“请求完成或失败时排队”字样。谢谢你的关注。由于提供此功能的委托方法在iOS 4.3中已被完全弃用,如何监控上载的进度?在这个类中看不到任何提供该功能的方法。在再次查看文档(对于OSX)后,看起来他们在OSX中保留了这些委托,但在iOS中没有。这里的文档:啊,我看到有一个不同的类可以使用。这看起来像是我正在寻找的。您还想测试
响应
状态码
是否为200。我确实很高兴使用这些东西。我正在使用NSURLConnection。iOS 4.3之后不包括您所谈论的委托方法。还有一些其他方法仍然存在,但它们不提供所需的功能。ASIHTTPRequest不再处于活动开发或受支持状态。我正在修改的代码实际上已经在使用GTM fetcher,但是如果它可以只使用本机代码,我希望删除该库。