2

我正在开发 iOS 本机应用程序以从 salesforce 获取附件。

我必须在我的 iPhone 应用程序中为特定对象(如线索、联系人等)显示销售人员附件。为此,我正在使用Rest Api并获得响应正文。但在响应正文中有 url 但我想要附件正文的二进制数据。

这是我的代码:

我的休息 api 请求

NSString *attachments=[NSString stringWithFormat:@"select Name,Body, ContentType from   Attachment"];
SFRestRequest *request = [[SFRestAPI sharedInstance] requestForQuery:attachments];
[[SFRestAPI sharedInstance] send:request delegate:self];

我得到以下格式的正文响应:

{
    Body = "/services/data/v23.0/sobjects/Attachment/00P90000004SRFlEAO/Body";
    ContentType = "application/video";
    Name = "Video.MOV";
    attributes =     {
       type = Attachment;
        url = "/services/data/v23.0/sobjects/Attachment/00P90000004SRFlEAO";
    };
}
4

3 回答 3

2

在获取正文 url 后使用此代码下载:

SFRestRequest* downloadRequest = [self requestForFileContents:@"/services/data/v23.0/sobjects/Attachment/00P90000004SRFlEAO/Body"];

- (SFRestRequest *) requestForFileContents:(NSString *) path {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
SFRestRequest *request = [SFRestRequest requestWithMethod:SFRestMethodGET path:path queryParams:params];
request.parseResponse = NO;
return request;}
于 2015-04-26T03:39:53.077 回答
1

您必须向 Body 字段中返回的 URL 发出 GET 请求,以获取实际的二进制内容。

于 2013-06-21T15:03:01.263 回答
0
Check this code:
id url = @"http://blogs.independent.co.uk/wp-content/uploads/2012/12/google-zip.jpg";
[self getImageBase64:url];


-( NSString *) AFBase64EncodedStringFromString: (NSData*) data
{
NSUInteger length = [data length];
NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];

uint8_t *input = (uint8_t *)[data bytes];
uint8_t *output = (uint8_t *)[mutableData mutableBytes];

for (NSUInteger i = 0; i < length; i += 3) {
    NSUInteger value = 0;
    for (NSUInteger j = i; j < (i + 3); j++) {
        value <<= 8;
        if (j < length) {
            value |= (0xFF & input[j]);
        }
    }

    static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    NSUInteger idx = (i / 3) * 4;
    output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
    output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
    output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
    output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
}

return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
}



-(NSString *) getImageBase64:(NSString *) url
{

NSURLRequest *         imageUrlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

NSURLResponse * response=nil;
NSError * error =nil;
NSData * data = [NSURLConnection sendSynchronousRequest:imageUrlRequest returningResponse:&response error:&error];

if(error == nil)
{
    return [self AFBase64EncodedStringFromString:data];
}

return nil;

}
于 2013-06-21T08:27:54.607 回答