1) If your cookies are not HTTP-only, you can get them by evaluating JavaScript command document.cookie
on webView
and use them for your request.
If they are HTTP-only, it seems that there is no working method to do this.
There is no documented way to sync cookies between WKWebView
and NSURLSession
as far as I know.
2) Another approach (if cookies belong to same domain which REST API belongs) - create "background" webView
with same process pool and perform all operations to REST API via this instance - cookies will be added automatically (even HTTP-only):
You can set any params (method/body/headers/etc) to request and load
it via loadRequest:
; If your REST API returns JSON, you can use JavaScript command
document.body
to get it;
Getting response code a little bit harder - you need to implement
webView:decidePolicyForNavigationResponse:
method of
WKNavigationDelegate
and catch
it.
Also, you need to keep in mind that you can perform only 1 request
in webView
simultaneously. So, you may need to create some queue
of requests that will be performed one after another.