I set cookie in request header before I call loadRequest() function to load a page. I also use document.cookie() to set cookie by WKUserScript according to [WKWebView Cookies. However, I find that if a 302 redirection occurs, request may fail for loss of cookie. For example, the request of http://A redirect to http://B, I could set cookie for request of http://A by operating request head and using WKUserScript, but these two ways can not set cookie for request of http://B, so the 302 request of http://B may fail. This situation occurs in ios8 more frequently than ios9. Does anybody have a workaround?
Asked
Active
Viewed 2,869 times
3
-
1Do you have a workaround for this problem now? – T. W Jun 15 '17 at 20:18
-
Yes, but it's not perfect and iOS 11 releases some new perfect api for developer to manage WKWebView cookie now. – Jun 16 '17 at 08:54
-
1Could you tell me the workaround? I had see the notes, but the app still need to change WKWebView on lower iOS. – T. W Jun 16 '17 at 14:02
-
1Yes, firstly, we add cookies to NSHTTPCookieStorage. Secondly, we detect whether cookie of request header is same as NSHTTPCookieStorage in a WKNavigationDelegate function - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler; If the cookie of request header is nil or not same as NSHTTPCookieStorage, we add the cookies from NSHTTPCookieStorage to request header, cancel the current request and loadRequest new request again. However, it's no perfect, it can not deal with all. – Jun 18 '17 at 02:33
-
I met a problem "Frame load interrupted", do you know why? When I search this error, it is for UIWebView. – T. W Jun 19 '17 at 20:16
-
May duplicate with [this answer](https://stackoverflow.com/questions/44576468/when-redirect-with-code-302-wkwebview-cannot-set-cookie/46822550#46822550). – Xingxing Oct 22 '17 at 06:21
2 Answers
0
Note sure, but probably the first response may contain "Set-Cookie" header. Hence, you have to use the provided cookie in the second request. May be it's missing.

Alexander Volkov
- 7,904
- 1
- 47
- 44
0
workaround for set cookies in iOS please check my answer. You must set cookies both in request and wkuserscript same time. otherwise it fail one time and sucess in 2nd run,

Muhammad Shauket
- 2,643
- 19
- 40