如何在objective-c中对日期进行url编码

时间:2013-08-08 07:45:38

标签: ios objective-c nsdate url-encoding

我需要 url编码日期参数,以便将其发送到api。

例如,如果我的日期为2013-08-01 00:00:00 +0000,我需要通过2013-08-01+00%3a00%3a00+%2b0000发送。{/ p>

我怎样才能实现它?

2 个答案:

答案 0 :(得分:2)

NSDate *myDate; // this date should be encoded
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"yyyy-MM-dd hh:mm:ss ZZZ";
NSString *dateString = [df stringFromDate:myDate];
NSString *encodedDate = 
  [dateString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

答案 1 :(得分:0)

这一直对我有用

NSString *encodedDate = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)dateString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);