iPhone开发--使用Obj-C对数据等进行URLEncoding编码
在Objective-c进行网络编程时,经常需要把数据转换成URLEncoding编码,如对+号编码后,变成%2b.这里我们给出一种实现.
[cpp] <span style="font-family:SimSun;font-size:13px;color:#330033;">//NSString+URLEncoding.h
#import
@interface NSString (URLEncodingAdditions)
- (NSString *)URLEncodedString;
- (NSString *)URLDecodedString;
@end</span>
<span style="font-family:SimSun;font-size:13px;color:#330033;">//NSString+URLEncoding.h
#import
@interface NSString (URLEncodingAdditions)
- (NSString *)URLEncodedString;
- (NSString *)URLDecodedString;
@end</span>
[cpp] <span style="font-family:SimSun;font-size:13px;color:#330033;">//NSString+URLEncoding.m
#import "NSString+URLEncoding.h"
@implementation NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString
{
NSString *result = (NSString *)
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR("!*'();:@&=+$,/?%#[] "),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
- (NSString*)URLDecodedString
{
NSString *result = (NSString *)
CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
(CFStringRef)self,
CFSTR(""),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
@end</span>
<span style="font-family:SimSun;font-size:13px;color:#330033;">//NSString+URLEncoding.m
#import "NSString+URLEncoding.h"
@implementation NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString
{
NSString *result = (NSString *)
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
相关新闻>>
- 发表评论
-
- 最新评论 更多>>