How do I URL encode a string

前端 未结 22 1942
轮回少年
轮回少年 2020-11-22 07:16

I have a URL string (NSString) with spaces and & characters. How do I url encode the entire string (including the & ampersand

22条回答
  •  时光取名叫无心
    2020-11-22 08:18

    Apple's advice, in the 10.11 release notes, is:

    If you need to percent-encode an entire URL string, you can use this code to encode a NSString intended to be a URL (in urlStringToEncode):

    NSString *percentEncodedURLString =
      [[NSURL URLWithDataRepresentation:[urlStringToEncode dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil] relativeString];
    

提交回复
热议问题