-
WKWebview 에서 post 요청하는방법개발/Swift 2025. 4. 8. 09:46
WKwebview를 사용하여 웹뷰를 로드할떄 load 함수를 사용함
webView.load(request)
이떄 들어가는 request는 URLRequest 타입이며
url 만 있으면 쉽게 만들수 있다.
let request = URLRequest(url: url, cachePolicy: cachePolicy)
일반적인 get 요청은 그런데 post 요청을 수행해야 하는 경우는 어떨까?
let request = URLRequest(url: url, cachePolicy: cachePolicy) request.httpMethod = "POST" webView.load(request)
httpMethod 만 지정해줘서 쉽게 바꿀수 있다.
이외 필요한 헤더 (토큰이나 contentType) 를 지정하고
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
바디에 정보 (파라미터) 를 답을수있다
let bodyParams = "username=test&password=1234" request.httpBody = bodyParams.data(using: .utf8) // Data타입
기존 url 에 파라미터가 있다면 query를 가져와서 넣어줄수도 있다
request.httpBody = url.query?.data(using: .utf8)
'개발 > Swift' 카테고리의 다른 글
[Swift] 애니메이션 정지 재생 CGAffineTransform (0) 2025.04.11 UIKit SwiftUI에서 자동 스크롤 (0) 2025.04.08 [SwiftUI] NavigationLink pop push 깜빡이는 문제 (0) 2025.04.08 SwiftUI 커스텀 팝업 노출 방법 fullScreenCover (0) 2025.03.25 TCA binding 액션 내부 타입 바인딩 @CasePathable (0) 2025.03.25