encode ios url
2013-10-28 11:26:00 weixin_33768481 阅读数 12
//ios Url Encode 
//有时候在请求的参数里里特殊符号比如“+”等。而如果没有encode的话那么传过去的还是”  ”,面实际上是%2B。
-(NSString*)UrlValueEncode:(NSString*)str
{
        NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                               (CFStringRef)str,
                                                                               NULL,
                                                                                  CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                               kCFStringEncodingUTF8);
       [result autorelease];
       return result;
}



//- (NSString*)URLDecodedString:(NSString*)str
//{
////    NSString *result = [[self stringByReplacingOccurrencesOfString:@"%0D%0A" withString:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSString *result = [[str stringByReplacingOccurrencesOfString:@"%0D%0A" withString:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    return [result stringByReplacingOccurrencesOfString:@"+" withString:@" "];
//}

 

2013-07-27 19:17:46 snsogblgbl 阅读数 818

用ASIFormDataRequest请求URl地址,当使用GET传参,参数是普通中文可以用  NSString 的 stringByAddingPercentEscapesUsingEncoding 可以对 url 参数进行编码。但是如果参数中包含特殊符号如 " & ? ! "等等,就不能用这种方法。

包含了特殊符号的可以使用ASIFormDataRequest 的 encodeURL 方法 如下使用;

ASIFormDataRequest* request = [[ASIFormDataRequest alloc] initWithURL:nil];
place_name = [request encodeURL:place_name];


encodeURl 方法如下:

- (NSString*)encodeURL:(NSString *)string
{
	NSString *newString = [NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding]))) autorelease];
	if (newString) {
		return newString;
	}
	return @"";
}




2016-09-07 13:40:41 jeffasd 阅读数 456

    //创建一个描述订单信息的JSON数据
    NSDictionary *dict = @{
                           @"shop_id" : @"123456",
                           @"shop_name" : @"bowen",
                           @"user_id" : @"2012110606"
                           };
    NSData *json = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];



iOS url 编码和解码

http://www.cnblogs.com/hanjun/archive/2013/05/02/3054667.html

1.url编码

ios中http请求遇到汉字的时候,需要转化成UTF-8,用到的方法是:

NSString * encodingString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

2.url解码

请求后,返回的数据,如何显示的是这样的格式:%3A%2F%2F,此时需要我们进行UTF-8解码,用到的方法是:

NSString *str = [model.album_name stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


2013-11-27 15:03:45 qingjoin1 阅读数 812
//ios Url Encode 
//有时候在请求的参数里里特殊符号比如“+”等。而如果没有encode的话那么传过去的还是”  ”,面实际上是%2B。
-(NSString*)UrlValueEncode:(NSString*)str
{
        NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                               (CFStringRef)str,
                                                                               NULL,
                                                                                  CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                               kCFStringEncodingUTF8);
       [result autorelease];
       return result;
}



//- (NSString*)URLDecodedString:(NSString*)str
//{
////    NSString *result = [[self stringByReplacingOccurrencesOfString:@"%0D%0A" withString:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSString *result = [[str stringByReplacingOccurrencesOfString:@"%0D%0A" withString:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    return [result stringByReplacingOccurrencesOfString:@"+" withString:@" "];
//}
2019-06-10 15:18:48 lg767201403 阅读数 13

iOS 9以前

URL Encode 用的是CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator, CFStringRef originalString, CFStringRef charactersToLeaveUnescaped, CFStringRef legalURLCharactersToBeEscaped, CFStringEncoding encoding) 这个方法。

而URL Decode用的是CFStringRef CFURLCreateStringByReplacingPercentEscapesUsingEncoding(CFAllocatorRef allocator, CFStringRef origString, CFStringRef charsToLeaveEscaped, CFStringEncoding encoding)这个方法。
(具体参数的含义有兴趣的可以自己搜搜,因为已经废除了,这里就不深入解释了)

此外还有两个苹果封装在NSString里面的方法

- (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
- (nullable NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)enc;

使用方法

+ (NSString*)urlEncode:(NSString*)text
{
    // 表示!*'();:@&=+$,/?%#[] 这些字符出现在URL里面时 会使用数字字母加%的形式代替
    NSString *result = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes
(kCFAllocatorDefault, (CFStringRef)text, NULL, CFSTR("!*'();:@&=+$,/?%#[]"),
 kCFStringEncodingUTF8);
    return result;
}


+ (NSString*)urlDecode:(NSString*)text
{
    NSString *result = (__bridge NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding
(kCFAllocatorDefault, (CFStringRef)text, CFSTR(""), kCFStringEncodingUTF8);
    return result;
}

// 编码一般都是UTF-8
- (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
- (nullable NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)enc;

但是这些方法都在iOS 9以后都被废除了,取而代之的是

- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters;
@property (nullable, readonly, copy) NSString *stringByRemovingPercentEncoding    // Decode

使用方法

@implementation NSString (Extension)

// 这里使用分类, self表示原字符串
- (NSString *)urlEncode{
    // !*'();:@&=+$,/?%#[]{}" 表示URL里遇到这些字符 将使用字母数字加%的形式代替
    NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:
@"!*'();:@&=+$,/?%#[]{}\""] invertedSet];
    return [self stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
}

- (NSString *)urlDecode{
    return [self stringByRemovingPercentEncoding];
}
@end

注意

在CFURLCreateStringByAddingPercentEscapes里面需要替换的字符是@"!*'();:@&=+$,/?%#[]" 但是在stringByAddingPercentEncodingWithAllowedCharacters里面设置同样的字符,两个方法得出来的结果是不一样的。原因是CFURLCreateStringByAddingPercentEscapes默认会处理花括号和双引号{}"这个三个字符。但是stringByAddingPercentEncodingWithAllowedCharacters不会。(曾因为这个问题导致签名失败,坑的我想哭)

iOS url进行encode

阅读数 117

iOS utf-8和 url encode

阅读数 4135

没有更多推荐了,返回首页