Replacement for stringByAddingPercentEscapesUsingEncoding in ios9?

IosObjective CUrlEncodingDeprecated

Ios Problem Overview


In iOS8 and prior I can use:

NSString *str = ...; // some URL
NSString *result = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

in iOS9 stringByAddingPercentEscapesUsingEncoding has been replaced with stringByAddingPercentEncodingWithAllowedCharacters:

NSString *str = ...; // some URL
NSCharacterSet *set = ???; // where to find set for NSUTF8StringEncoding?
NSString *result = [str stringByAddingPercentEncodingWithAllowedCharacters:set];

and my question is: where to find needed NSCharacterSet (NSUTF8StringEncoding) for proper replacement of stringByAddingPercentEscapesUsingEncoding?

Ios Solutions


Solution 1 - Ios

The deprecation message says (emphasis mine):

> Use stringByAddingPercentEncodingWithAllowedCharacters(_:) instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.

So you only need to supply an adequate NSCharacterSet as argument. Luckily, for URLs there's a very handy class method called URLHostAllowedCharacterSet that you can use like this:

let encodedHost = unencodedHost.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

Update for Swift 3 -- the method becomes the static property urlHostAllowed:

let encodedHost = unencodedHost.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

Be aware, though, that:

> This method is intended to percent-encode an URL component or subcomponent string, NOT an entire URL string.

Solution 2 - Ios

For Objective-C:

NSString *str = ...; // some URL
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; 
NSString *result = [str stringByAddingPercentEncodingWithAllowedCharacters:set];

where to find set for NSUTF8StringEncoding?

There are predefined character sets for the six URL components and subcomponents which allow percent encoding. These character sets are passed to -stringByAddingPercentEncodingWithAllowedCharacters: .

 // Predefined character sets for the six URL components and subcomponents which allow percent encoding. These character sets are passed to -stringByAddingPercentEncodingWithAllowedCharacters:.
@interface NSCharacterSet (NSURLUtilities)
+ (NSCharacterSet *)URLUserAllowedCharacterSet;
+ (NSCharacterSet *)URLPasswordAllowedCharacterSet;
+ (NSCharacterSet *)URLHostAllowedCharacterSet;
+ (NSCharacterSet *)URLPathAllowedCharacterSet;
+ (NSCharacterSet *)URLQueryAllowedCharacterSet;
+ (NSCharacterSet *)URLFragmentAllowedCharacterSet;
@end

The deprecation message says (emphasis mine):

> Use stringByAddingPercentEncodingWithAllowedCharacters(_:) instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.

So you only need to supply an adequate NSCharacterSet as argument. Luckily, for URLs there's a very handy class method called URLHostAllowedCharacterSet that you can use like this:

NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; 

Be aware, though, that:

> This method is intended to percent-encode an URL component or subcomponent string, NOT an entire URL string.

Solution 3 - Ios

URLHostAllowedCharacterSet is NOT WORKING FOR ME. I use URLFragmentAllowedCharacterSet instead.

OBJECTIVE -C

NSCharacterSet *set = [NSCharacterSet URLFragmentAllowedCharacterSet];
NSString * encodedString = [@"url string" stringByAddingPercentEncodingWithAllowedCharacters:set];

SWIFT - 4

"url string".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

The following are useful (inverted) character sets:

URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
URLHostAllowedCharacterSet      "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet     "#%<>[\]^`{|}
URLUserAllowedCharacterSet      "#%/:<>?@[\]^`

Solution 4 - Ios

Objective-C

this code work for me :

urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

Solution 5 - Ios

Swift 2.2:

extension String {
 func encodeUTF8() -> String? {
//If I can create an NSURL out of the string nothing is wrong with it
if let _ = NSURL(string: self) {

    return self
}

//Get the last component from the string this will return subSequence
let optionalLastComponent = self.characters.split { $0 == "/" }.last


if let lastComponent = optionalLastComponent {

    //Get the string from the sub sequence by mapping the characters to [String] then reduce the array to String
    let lastComponentAsString = lastComponent.map { String($0) }.reduce("", combine: +)


    //Get the range of the last component
    if let rangeOfLastComponent = self.rangeOfString(lastComponentAsString) {
        //Get the string without its last component
        let stringWithoutLastComponent = self.substringToIndex(rangeOfLastComponent.startIndex)


        //Encode the last component
        if let lastComponentEncoded = lastComponentAsString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet()) {


        //Finally append the original string (without its last component) to the encoded part (encoded last component)
        let encodedString = stringWithoutLastComponent + lastComponentEncoded

            //Return the string (original string/encoded string)
            return encodedString
        }
    }
}

return nil;
}
}

Solution 6 - Ios

For Swift 3.0

You can use urlHostAllowed characterSet.

> /// Returns the character set for characters allowed in a host URL subcomponent.

public static var urlHostAllowed: CharacterSet { get }

WebserviceCalls.getParamValueStringForURLFromDictionary(settingsDict as! Dictionary<String, AnyObject>).addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)

Solution 7 - Ios

> What is the meaning of "This method is intended to percent-encode an URL component or subcomponent string, NOT an entire URL string." ? – GeneCode Sep 1 '16 at 8:30

It means that you are not supposed to encode the https://xpto.example.com/path/subpath of the url, but only what goes after the ?.

Supposed, because there are use-cases for doing it in cases like:

https://example.com?redirectme=xxxxx

Where xxxxx is a fully encoded URL.

Solution 8 - Ios

Adding to the accepted answer. Taking into consideration this note

> This method is intended to percent-encode an URL component or subcomponent string, NOT an entire URL string.

the whole URL should not be encoded:

let param = "=color:green|\(latitude),\(longitude)&\("zoom=13&size=\(width)x\(height)")&sensor=true&key=\(staticMapKey)".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) 
let url = "https://maps.google.com/maps/api/staticmap?markers" + param!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionslavikView Question on Stackoverflow
Solution 1 - IosAntonio FavataView Answer on Stackoverflow
Solution 2 - IosChenYilongView Answer on Stackoverflow
Solution 3 - IosLal KrishnaView Answer on Stackoverflow
Solution 4 - IosBilal LView Answer on Stackoverflow
Solution 5 - IosBobj-CView Answer on Stackoverflow
Solution 6 - IostechnerdView Answer on Stackoverflow
Solution 7 - IoskindaianView Answer on Stackoverflow
Solution 8 - IosAyman IbrahimView Answer on Stackoverflow