`
linwwwei
  • 浏览: 222793 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

CFURLCreateStringByAddingPercentEscapes

 
阅读更多
If you have tried to send any information using a GET web request, you would have come cross an annoying problem, That annoying problem is making sure that the URL is corrently encoded.

  The issue is that by default most of these methods leave characters such as & = ? within a URL, as they are strictly speaking valid. The problem is that these characters have special meanings in a GET request, and will more than likely make your request invalid.
  也就是说,你提供的 URL 字符串 里面可能包含某些字符,比如‘$‘ ‘&’ ‘?’...等,这些字符在 URL 语法中是具有特殊语法含义的,
比如 URL :http://www.baidu.com/s?wd=%BD%AA%C3%C8%D1%BF&rsv_bp=0&rsv_spt=3&inputT=3512
中 的 & 起到分割作用 等等,如果 你提供的URL 本身就含有 这些字符,就需要把这些字符 转化为 “%+ASCII” 形式,以免造成冲突。
  这就引入:CFURLCreateStringByAddingPercentEscapes 函数。
  该函数将 将要添加到URL的字符串进行特殊处理,如果这些字符串含有 &, ? 这些特殊字符,用“%+ASCII” 代替之。
CFURLCreateStringByAddingPercentEscapes(
  kCFAllocatorDefault,
  (CFStringRef)parameter,
  NULL,
   CFSTR(":/?#[]@!$&’()*+,;="),     // 确定 parameter 字符串中含有:/?#[]@!$&’()*+,;=这些字符时候,这些字符需要被转化,以免与语法冲突。
  kCFStringEncodingUTF8
);
@implementation NSURL (mm)

+ (NSURL *)URLWithBaseString:(NSString *)baseString parameters:(NSDictionary *)parameters{  
   
    NSMutableString *urlString =[NSMutableString string];   //The URL starts with the base string[urlString appendString:baseString];  

    [urlString appendString:baseString];

    NSString *escapedString;  

    NSInteger keyIndex = 0;  
   
    for (id key in parameters) {  
     
      //First Parameter needs to be prefixed with a ? and any other parameter needs to be prefixed with an &
      if(keyIndex ==0) {
          escapedString =(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)[parameters valueForKey:key], NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8);
          [urlString appendFormat:@"?%@=%@",key,escapedString];
          [escapedString release];
      }else{  
          escapedString =(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)[parameters valueForKey:key], NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8);  
         
          [urlString appendFormat:@"&%@=%@",key,escapedString];
          [escapedString release]; 
      }  
      keyIndex++;
    }  
    return [NSURL URLWithString:urlString];  
}
@end

测试:NSString * baseString = @"http://twitter.com/statuses/update.xml";
    NSDictionary*dictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"This is my status",@"status",@"meng ya", @"meyers",nil];
    NSURL * url = [NSURL URLWithBaseString:baseString parameters:dictionary];
    NSLog(@"the url : %@", url);
输出:the url : http://twitter.com/statuses/update.xml?status=This%20is%20my%20status&meyers=meng%20ya
分享到:
评论

相关推荐

    项目中的问题

    - 利用Core Foundation框架中的`CFURLCreateStringByAddingPercentEscapes`函数进行转换。 #### 四、键盘视图调整 当应用程序中的视图与键盘交互时,视图的位置可能需要根据键盘的变化而实时调整。 **解决建议:*...

    IOS常用文档

    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8); [result autorelease];...

    iphone开发常用代码

    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8 ); [result ...

    iOS 开发笔记

    NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)self, (CFStringRef)@"!$&'()*+,-./:;=?@_~%", NULL, kCFStringEncodingUTF8 ); ...

    IOS中对Url进行编码和解码示例

    NSString *outputStr = (NSString *)CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, /* allocator */ (CFStringRef)input, NULL, /* charactersToLeaveUnescaped */ (CFStringRef)@"!*'();:@&...

    IOS 常见内存泄漏以及解决方案

    例如,使用 CFURLCreateStringByAddingPercentEscapes 函数将 OC 字符串转换为 CF 字符串时,需要手动释放 CF 对象,否则将导致内存泄漏。 解决办法:在使用 CF 对象时,需要手动释放对象,使用 CFRelease 函数来...

Global site tag (gtag.js) - Google Analytics