Make an NSURL with an encoded plus (+)

前端 未结 7 1155
北海茫月
北海茫月 2021-02-14 01:32

I need to pass a timestamp with a timezone offset in a GET request, e.g.,

2009-05-04T11:22:00+01:00

This looks like a two arguments

7条回答
  •  一向
    一向 (楼主)
    2021-02-14 01:51

    Solution when using URLComponents (Swift 3):

    var params = ["email": "user+ios-default@example.com", "name": "John Brown"]
    var components = URLComponents(string: "http://www.example.com")!
    components.path = "/login"
    components.queryItems = params.map { URLQueryItem(name: $0, value: $1) }
    
    let url_NoFix = components.url!
    // http://www.example.com/login?name=John%20Brown&email=user+ios-default@example.com
    
    let cs = CharacterSet(charactersIn: "+").inverted
    let q =  components.percentEncodedQuery?.addingPercentEncoding(withAllowedCharacters: cs)
    components.percentEncodedQuery = q
    
    let url_Fixed = components.url!
    // http://www.example.com/login?name=John%20Brown&email=user%2Bios-default@example.com
    

提交回复
热议问题