티스토리 뷰

swift 에서 제공하는 퍼센트 인코딩 옵션이 있습니다.

 

그런데 인터넷에 잘못된 정보가 너무 많더라구요..

 

직접 확인하실 수 있는 코드를 소개합니다.

 

extension CharacterSet {
    func allCharacters() -> [Character] {
        var result: [Character] = []
        for plane: UInt8 in 0...16 where self.hasMember(inPlane: plane) {
            for unicode in UInt32(plane) << 16 ..< UInt32(plane + 1) << 16 {
                if let uniChar = UnicodeScalar(unicode), self.contains(uniChar) {
                    result.append(Character(uniChar))
                }
            }
        }
        return result
    }
}


let set: [CharacterSet] = [
    .urlUserAllowed,
    .urlPasswordAllowed,
    .urlHostAllowed,
    .urlPathAllowed,
    .urlQueryAllowed,
    .urlFragmentAllowed,
]


set.forEach{
    $0.allCharacters()
        .filter { !$0.isLetter && !$0.isNumber }
        .forEach { print($0, terminator: " ") }
    print("")
}


//! $ & ' ( ) * + , - . ; = _ ~
//! $ & ' ( ) * + , - . ; = _ ~
//! $ & ' ( ) * + , - . : ; = [ ] _ ~
//! $ & ' ( ) * + , - . / : = @ _ ~
//! $ & ' ( ) * + , - . / : ; = ? @ _ ~
//! $ & ' ( ) * + , - . / : ; = ? @ _ ~

해당 문자열은 인코딩하지 않으니 필요에 따라 선택하시면될 것 같습니다.

 

 

RFC 3986 기준 인코딩 방식입니다.

extension String {
  func stringByAddingPercentEncodingForRFC3986() -> String? {
    let unreserved = "-._~/?"
    let allowed = NSMutableCharacterSet.alphanumeric()
    allowed.addCharacters(in: unreserved)
    return addingPercentEncoding(withAllowedCharacters: allowed as CharacterSet)
  }
}

참고

https://useyourloaf.com/blog/how-to-percent-encode-a-url-string/

 

참고
https://www.rfc-editor.org/rfc/rfc3986#section-3.4

3.4. Query
The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority (if any). The query component is indicated by the first question mark ("?") character and terminated by a number sign ("#") character or by the end of the URI.
query = *( pchar / "/" / "?" )
The characters slash ("/") and question mark ("?") may represent data within the query component. Beware that some older, erroneous implementations may not handle such data correctly when it is used as the base URI for relative references (Section 5.1), apparently because they fail to distinguish query data from path data when looking for hierarchical separators. However, as query components are often used to carry identifying information in the form of "key=value" pairs and one frequently used value is a reference to another URI, it is sometimes better for usability to avoid percent- encoding those characters.

 

Alamofire 참고

extension CharacterSet {
    /// Creates a CharacterSet from RFC 3986 allowed characters.
    ///
    /// RFC 3986 states that the following characters are "reserved" characters.
    ///
    /// - General Delimiters: ":", "#", "[", "]", "@", "?", "/"
    /// - Sub-Delimiters: "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="
    ///
    /// In RFC 3986 - Section 3.4, it states that the "?" and "/" characters should not be escaped to allow
    /// query strings to include a URL. Therefore, all "reserved" characters with the exception of "?" and "/"
    /// should be percent-escaped in the query string.
    public static let afURLQueryAllowed: CharacterSet = {
        let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
        let subDelimitersToEncode = "!$&'()*+,;="
        let encodableDelimiters = CharacterSet(charactersIn: "\(generalDelimitersToEncode)\(subDelimitersToEncode)")

        return CharacterSet.urlQueryAllowed.subtracting(encodableDelimiters)
    }()
}
댓글
공지사항