Issue with encoding “&” in URLs

后端 未结 3 708
挽巷
挽巷 2021-01-20 01:21

I\'ve got to send a https GET request to a web service in my iPhone app which is developing in Swift 1.2.

I am trying to construct query string parameters but got to

3条回答
  •  面向向阳花
    2021-01-20 01:55

    I used such an utility method to URL-encode values in GET-requests:

    @interface NSString (Ext)
    
    @property (nonatomic, readonly) NSString *urlEncoded;
    
    @end
    
    @implementation NSString (Ext)
    
    - (NSString *)urlEncoded {
        NSMutableCharacterSet *const allowedCharacterSet = [NSCharacterSet URLQueryAllowedCharacterSet].mutableCopy;
        // See https://en.wikipedia.org/wiki/Percent-encoding
        [allowedCharacterSet removeCharactersInString:@"!*'();:@&=+$,/?#[]"]; // RFC 3986 section 2.2 Reserved Characters (January 2005)
        NSString *const urlEncoded = [self stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
        return urlEncoded;
    }
    
    @end
    

提交回复
热议问题