How do I URL encode a string

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

    Swift 2.0 Example (iOS 9 Compatiable)

    extension String {
    
      func stringByURLEncoding() -> String? {
    
        let characters = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as! NSMutableCharacterSet
    
        characters.removeCharactersInString("&")
    
        guard let encodedString = self.stringByAddingPercentEncodingWithAllowedCharacters(characters) else {
          return nil
        }
    
        return encodedString
    
      }
    
    }
    
    0 讨论(0)
  • 2020-11-22 08:11

    Here's a production-ready flexible approach in Swift 5.x:

    public extension CharacterSet {
    
        static let urlQueryParameterAllowed = CharacterSet.urlQueryAllowed.subtracting(CharacterSet(charactersIn: "&?"))
    
        static let urlQueryDenied           = CharacterSet.urlQueryAllowed.inverted()
        static let urlQueryKeyValueDenied   = CharacterSet.urlQueryParameterAllowed.inverted()
        static let urlPathDenied            = CharacterSet.urlPathAllowed.inverted()
        static let urlFragmentDenied        = CharacterSet.urlFragmentAllowed.inverted()
        static let urlHostDenied            = CharacterSet.urlHostAllowed.inverted()
    
        static let urlDenied                = CharacterSet.urlQueryDenied
            .union(.urlQueryKeyValueDenied)
            .union(.urlPathDenied)
            .union(.urlFragmentDenied)
            .union(.urlHostDenied)
    
    
        func inverted() -> CharacterSet {
            var copy = self
            copy.invert()
            return copy
        }
    }
    
    
    
    public extension String {
        func urlEncoded(denying deniedCharacters: CharacterSet = .urlDenied) -> String? {
            return addingPercentEncoding(withAllowedCharacters: deniedCharacters.inverted())
        }
    }
    

    Example usage:

    print("Hello, World!".urlEncoded()!)
    print("You&Me?".urlEncoded()!)
    print("#Blessed 100%".urlEncoded()!)
    print("Pride and Prejudice".urlEncoded(denying: .uppercaseLetters)!)
    

    Output:

    Hello,%20World!
    You%26Me%3F
    %23Blessed%20100%25
    %50ride and %50rejudice
    
    0 讨论(0)
  • 2020-11-22 08:11

    Try to use stringByAddingPercentEncodingWithAllowedCharacters method with [NSCharacterSet URLUserAllowedCharacterSet] it will cover all the cases

    Objective C

    NSString *value = @"Test / Test";
    value = [value stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLUserAllowedCharacterSet]];
    

    swift

    var value = "Test / Test"
    value.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLUserAllowedCharacterSet())
    

    Output

    Test%20%2F%20Test

    0 讨论(0)
  • 2020-11-22 08:11

    In my case where the last component was Arabic letters I did the following in 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;
    }
    }
    

    usage:

    let stringURL = "http://xxx.dev.com/endpoint/nonLatinCharacters"
    
    if let encodedStringURL = stringURL.encodeUTF8() {
    
        if let url = NSURL(string: encodedStringURL) {
    
          ...
        }
    
    } 
    
    0 讨论(0)
  • 2020-11-22 08:13

    New APIs have been added since the answer was selected; You can now use NSURLUtilities. Since different parts of URLs allow different characters, use the applicable character set. The following example encodes for inclusion in the query string:

    encodedString = [myString stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
    

    To specifically convert '&', you'll need to remove it from the url query set or use a different set, as '&' is allowed in a URL query:

    NSMutableCharacterSet *chars = NSCharacterSet.URLQueryAllowedCharacterSet.mutableCopy;
    [chars removeCharactersInRange:NSMakeRange('&', 1)]; // %26
    encodedString = [myString stringByAddingPercentEncodingWithAllowedCharacters:chars];
    
    0 讨论(0)
  • 2020-11-22 08:14

    This might be helpful

    NSString *sampleUrl = @"http://www.google.com/search.jsp?params=Java Developer";
    NSString* encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding:
     NSUTF8StringEncoding];
    

    For iOS 7+, the recommended way is:

    NSString* encodedUrl = [sampleUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    

    You can choose the allowed character set as per the requirement of the URL component.

    0 讨论(0)
提交回复
热议问题