[英]Sending attachment to comment in Zendesk (iOS)
我正在使用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];
}
}];
}
有什么建議么?
我通過使用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];
}
}
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.