如何在ios中访问Salesforce Attachment Body(base64二进制数据)?

时间:2013-06-21 06:08:33

标签: iphone ios objective-c salesforce nt-native-api

我正在使用iOS本机应用程序来获取salesforce的附件。

我必须在iPhone应用程序中显示salesforce附件中的特定对象,例如Leads,Contacts等。为此,我使用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";
    };
}

3 个答案:

答案 0 :(得分:2)

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

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/world
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at javademo4.driver.main(driver.java:8)

答案 1 :(得分:1)

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

答案 2 :(得分: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;

}