2017-05-31

NSURLComponents 를 이용한 NSURLQueryItem 사용시 URL Encode


NSURLComponents 를 이용한 NSURLQueryItem 사용시 URL Encode

iOS 8 이상에서 NSURLQueryItem를 사용해서 get 방식으로 보낼 url query key/value를 작성하게 되는데, 이 경우 자체적으로 URL encode를 하기 때문에 따로 urlencode 코드를 넣어주면 2중 encode가 되어 버린다.

https://developer.apple.com/reference/foundation/nsurlqueryitem/1572045-queryitemwithname?language=objc

개발자 문서 아래 Discussion 부분을 보면 key/value 모두 자동으로 인코딩을 하게 되므로  따로 하지 말고 한다.

To use the newly initialized query item in composing a URL, add it to the queryItems array of an NSURLComponents instance. Because assigning an array of query items to an NSURLComponents instance automatically encodes the name and value properties, you should not percent-encode these strings.

이는 ​iOS8 이상에서만 사용 가능한 class 이므로 iOS7 이하는 url query method를 구성 하고 필요한 경우 url encode 를 적용해야 한다.​


댓글 없음:

댓글 쓰기