ios - 在 Zendesk (iOS) 中发送评论附件

标签 ios image rest attachment zendesk

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

关于ios - 在 Zendesk (iOS) 中发送评论附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30376329/

相关文章:

ios - 在 Storyboard中设置约束时,Xcode 不断崩溃

ios - 触摸开始 : in UIWebView

java - 使用 Java 对 HP ALM 12 REST API 进行身份验证

ios - 如何设置所有 View Controller 导航栏的默认导航栏颜色、文本颜色

c++ - 从 HBITMAP 转换为 IWICBitmap

php - 显示数据库中存储的图像

HTML5 Canvas ,调整背景图像的大小

javascript - 如何正确处理 REST API 的路由

java - Netty 与 RestEasy 的简单使用

ios - 应用被拒——如何证明没有使用私有(private)API