【问题标题】:how to access Salesforce Attachment Body (base64 binary data) in ios?如何在 ios 中访问 Salesforce 附件正文(base64 二进制数据)?
【发布时间】:2013-06-18 04:46:43
【问题描述】:

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

我必须在我的 iPhone 应用程序中为特定对象(如线索、联系人等)显示 salesforce 附件。为此,我使用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";
    };
}

【问题讨论】:

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


    【解决方案1】:

    获取body 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;}
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 感谢您的回答,但请澄清我到底要做什么,哪些参数需要与 URL 请求一起传递。我应该像实例 URL + 附件正文 url 一样请求这个 URL
      【解决方案3】:
      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;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        • 2019-11-24
        • 2015-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        相关资源
        最近更新 更多