发送附件以在 Zendesk 中发表评论 (iOS)

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];
        }
    }
}];