LoginSignup
3
1

More than 5 years have passed since last update.

WKWebViewにPOSTリクエストを送信した際にBodyが無くなる問題がiOS11で解消していた模様

Posted at

iOSのWKWebViewでは以下のようにURLRequestでPOSTリクエストを送った場合にBodyが無くなるという問題(仕様?)がありました。
Appleのフォーラムなどでも指摘されています。

実装例

func roguinjikkou() {
    var request = URLRequest(url: kWebStoreLoginUrl)
    request.httpMethod = "POST"
    let postString = "userId=kintaro%40example.co.jp&password=momotaro"
    request.httpBody = postString.data(using: .utf8)
    self.webView.load(request)
}

今回、いろいろ必要にかられて検証しているなかで、iOS11ではボディが無くならずに正常にPOSTリクエストが送れることを発見しました。

以下、OS毎のリクエストの通信ログの例です。(iOSシミュレータでの実行結果です。)

iOS 11

POST /Login HTTP/1.1
Host: test.example.co.jp
Origin: null
Content-Type: application/x-www-form-urlencoded
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0_1 like Mac OS X) AppleWebKit/604.2.10 (KHTML, like Gecko) Mobile/15A8401
Content-Length: 160
Accept-Language: en-us
Accept-Encoding: br, gzip, deflate

userId=kintaro%40example.co.jp&password=momotaro

iOS 10

POST /Login HTTP/1.1
Host: test.example.co.jp
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin: null
Connection: keep-alive
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E8301
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Content-Length: 0

iOS 9

POST /Login HTTP/1.1
Host: test.example.co.jp
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin: null
Connection: keep-alive
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13B143
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Content-Length: 0

iOS10, iOS9はPOSTのボディがなくなっているのがわかります。
ちなみに、UIWebViewではどのバージョンのOSでもボディは無くなりませんでした。

簡単にまとめると以下のようになります。

WKWebView UIWebView
iOS9系 消える 消えない
iOS10系 消える 消えない
iOS11系 消えない 消えない

iOS11ではCookieまわりのクラスも充実してきているため、iOS11以降の端末のみをサポートできるようになれば、WebViewまわりの制御は実装しやすくなりそうです。

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1