我正在使用Zendesk开发iOS应用程序,我使用REST v2 api并且我对注释的附件有问题。发送附件的操作看起来很好,但是当尝试从评论中读取附件时我遇到了问题,因为文件已损坏(我发送图像)。我正在使用AFNetworking库。这是我的代码:
- (void)addAttachment:(NSData*)data withFileName:(NSString*)fileName {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:API_USER password:API_TOKEN];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];
[manager.requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
NSDictionary *parameters = @{@"image":@{ @"content_type": @"image/jpeg", @"filename":fileName, @"file_data": [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]}};
[manager POST:[NSString stringWithFormat:@"%@uploads.json?filename=%@", API_URL, fileName] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dictionary = responseObject;
if (dictionary != nil && [dictionary objectForKey:@"upload"] != nil) {
NSString *token = [[dictionary objectForKey:@"upload"] objectForKey:@"token"];
if ([self.delegate respondsToSelector:@selector(didFinishedAddAttachmentWithSuccess:andToken:)]) {
[self.delegate didFinishedAddAttachmentWithSuccess:YES andToken:token];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
if ([self.delegate respondsToSelector:@selector(didFinishedAddAttachmentWithSuccess:andToken:)]) {
[self.delegate didFinishedAddAttachmentWithSuccess:NO andToken:nil];
}
}];
}
有什么建议吗?
答案 0 :(得分:2)
我使用Zendesk Mobile SDK解决了这个问题:
ZDKUploadProvider *uploadProvider = [[ZDKUploadProvider alloc] init];
[uploadProvider uploadAttachment:data withFilename:fileName andContentType:@"image/jpg" callback:^(ZDKUploadResponse *uploadResponse, NSError *error) {
if (uploadResponse != nil && [self.delegate respondsToSelector:@selector(didFinishedAddAttachmentWithSuccess:andToken:)]) {
[self.delegate didFinishedAddAttachmentWithSuccess:YES andToken:uploadResponse.uploadToken];
}
else {
if ([self.delegate respondsToSelector:@selector(didFinishedAddAttachmentWithSuccess:andToken:)]) {
[self.delegate didFinishedAddAttachmentWithSuccess:NO andToken:nil];
}
}
}];