How do I URL encode a string

前端 未结 22 1710
轮回少年
轮回少年 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:08

    For individual www form-encoded query parameters, I made a category on NSString:

    - (NSString*)WWWFormEncoded{
         NSMutableCharacterSet *chars = NSCharacterSet.alphanumericCharacterSet.mutableCopy;
         [chars addCharactersInString:@" "];
         NSString* encodedString = [self stringByAddingPercentEncodingWithAllowedCharacters:chars];
         encodedString = [encodedString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
         return encodedString;
    }
    

提交回复
热议问题