String 에서 기본적으로 제공하는 메소드를 사용하여 손쉽게 인코딩을 할수가 있다.
let str = "인코딩할 내용";
let encodedStr = str.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
withAllowedCharacters 값으로 보통은 urlQueryAllowed 값을 사용하게된다.
https://developer.apple.com/documentation/foundation/characterset#2902136
CharacterSet | Apple Developer Documentation
A set of Unicode character values for use in search operations.
developer.apple.com
애플 도큐멘트를 보면 항목들이 나오지만 뭐가 인코딩되는지 정확히 알지 못했다.
아래 설명은 해당 CharacterSet 을 선택하면 나열된 문자를 제외하고 인코딩이 되어진다.
상황에 맞게 골라서 사용하면 되겠다.
.urlUserAllowed
! $ & \ ( ) * + , - . ; = _ ~
.urlPasswordAllowed
! $ & \ ( ) * + , - . ; = _ ~
.urlPathAllowed
! $ & \ ( ) * + , - . / : = @ _ ~
.urlHostAllowed
! $ & \ ( ) * + , - . : ; = [ ] _ ~
.urlFragmentAllowed
! $ & \ ( ) * + , - . / : ; = ? @ _ ~
.urlQueryAllowed
! $ & \ ( ) * + , - . / : ; = ? @ _ ~
'iOS' 카테고리의 다른 글
[iOS] Xcode 14.3 아카이브 시 이슈 발생 (PhaseScriptExecution failed with a nonzero exit code) (0) | 2023.06.15 |
---|---|
[iOS,Swift] App과 Web 통신하기 (Webkit MessageHandlers) (0) | 2023.06.15 |
[iOS,Swift] 스트리밍 URL로 비디오영상 재생하기 (0) | 2023.05.08 |
[iOS,Swift] WebView 파일 다운로드 구현하기 (0) | 2022.07.21 |