objective-c - AFNetworking 如何检查上传操作是否完成或失败

标签 objective-c upload notifications afnetworking

如何检查 AFNetworking 是否已完成文件上传?我如何检查是否存在问题?

这是我的代码:

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://server"]];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:[fieldName text] forKey:@"name"];
[parameters setObject:[fieldSurname text] forKey:@"surname"];

NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:[[AttachedFile sharedAttachedFile]dataToSend] mimeType:@"image/jpeg" name:@"attach"];
}];

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:myRequest] autorelease]; 
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];

我应该实现哪个方法(和委托(delegate))?

谢谢!

最佳答案

使用

[AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest
                                                success:(void (^)(id object))success 
                                                failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure];

而不是

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:myRequest] autorelease]; 

当您的文件完全上传时,将调用成功 block 。如果出现问题,则会调用故障 block 。

关于objective-c - AFNetworking 如何检查上传操作是否完成或失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8037341/

相关文章:

android - android 8.0的通知振动问题

php - C2DM 和 GCM 有什么区别

ios - UINavigationController 上的 UIBarButton 因此适用于所有 subview

iphone - 在 Objective-C 中以编程方式计算 IRR(内部 yield )和 NPV

objective-c - 使 CALayer 及其内部的所有东西成为半透明的?

image - HTML5 上传前调整图片大小

objective-c - UIDocumentInteractionController与presentOpenInMenuFromBarButtonItem:方法一起崩溃

upload - 如何使用 CasperJS 通过普通 Javascript 进行文件 POST,而不是通过 UI

ios - 在上传网络更改期间,iOS

java - 无法解释来自 GCM 的消息