user1898829
user1898829

Reputation: 3527

How do I url encode a date in objective-c

I need to url encode a date parameter so I can send it to the api.

For example if I have a date 2013-08-01 00:00:00 +0000, I need to send this through 2013-08-01+00%3a00%3a00+%2b0000.

How can I achieve it?

Upvotes: 0

Views: 1475

Answers (2)

wingNut86
wingNut86

Reputation: 54

This has always worked for me

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

Upvotes: 0

Mundi
Mundi

Reputation: 80273

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];

Upvotes: 2

Related Questions