Iphone 如何检测NSURLConnection sendAsynchronousRequest是否已完成
我正在使用sendAsynchronousRequest:queue:completionHandler:上载一个文件(我已经删除了一些旧的第三方库,以支持使用此本机方法进行直接调用。我保留了NSURLRequest和字典)。我不知道如何告诉它是如何完成推送文件的。我想在completionHandler中,它将定期调用,并且我们可以检查传入的参数,但是它们似乎不包含我需要的内容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
- (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,则操作成功(不确定为什么要测试数据长度) - 那么使用它呢。。然后使用它的
- 或者使用框架
响应
的状态码
是否为200。我确实很高兴使用这些东西。我正在使用NSURLConnection。iOS 4.3之后不包括您所谈论的委托方法。还有一些其他方法仍然存在,但它们不提供所需的功能。ASIHTTPRequest不再处于活动开发或受支持状态。我正在修改的代码实际上已经在使用GTM fetcher,但是如果它可以只使用本机代码,我希望删除该库。