wkwebview에서 http request post 호출이 되지 않기 떄문에
임의로 post용 html 을 생성 한 후 javascript를 이용해 post로 넘기는 방법으로 처리한다.
func makePostRequest(url: String, payload: Dictionary<String, Any>) {
let jsonPayload: String
do {
let data = try JSONSerialization.data(
withJSONObject: payload,
options: JSONSerialization.WritingOptions(rawValue: 0))
jsonPayload = String(data: data, encoding: String.Encoding.utf8)!
} catch {
jsonPayload = "{}"
}
webView.loadHTMLString(postMakingHTML(url: url, payload: jsonPayload), baseURL: nil)
}
private func postMakingHTML(url: String, payload: String) -> String {
return "<html><head><script>function post(path,params,method){method = method || 'post';var form=document.createElement('form');form.setAttribute('method', method);form.setAttribute('action',path);for(var key in params){if(params.hasOwnProperty(key)){var hiddenField=document.createElement('input');hiddenField.setAttribute('type', 'hidden');hiddenField.setAttribute('name', key);hiddenField.setAttribute('value', params[key]);form.appendChild(hiddenField);}}document.body.appendChild(form);form.submit();}</script></head><body></body></html><script>post('\(url)',\(payload),'post');</script>"
}
출처 : https://blog.brainsandbeards.com/debugging-wkwebviews-5b122091db00
'Development > iPhone' 카테고리의 다른 글
Find numbers in string in swift (0) | 2019.07.02 |
---|---|
GADInvalidInitializationException occurred on abom in ios or on google ad manager (0) | 2019.03.22 |
IAP Receipt validation function (0) | 2018.08.07 |
ios https 연결시 1200 에러 노출 테스트 (0) | 2018.06.12 |