添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
酷酷的猴子  ·  Error deploying my ...·  3 小时前    · 
奔跑的鼠标垫  ·  PSA: New stable ...·  6 月前    · 
温暖的雪糕  ·  QSvgRenderer — Qt for ...·  6 月前    · 
年轻有为的猴子  ·  CI: Numpy 1.22 caused ...·  7 月前    · 

Hi, I'm trying building an (iOS) app based on WkWebView.
On the first launch, when I use the (web-based) login form, it doesn't work at all, all cookies are dropped!

After I "pause" the app (by hitting the Home button) or after it's been killed, I can use the login form and it works normally. (Well, almost normally, since only one cookie out of 4 is kept, but it's the only mandatory one...)

This behaviour, with my code, is fully reproductible, I've spent about 2 days (full-time) on this, trying to make things work. If I use UIWebView instead, it works fine (except that there are the weird things that come with UI, such as the scrolling events that are not triggered properly). If I use apache/cordova-plugin-wkwebview-engine , it's the exact same broken behaviour.

I'm gonna fall back to UIWebView until I find a fix for it. But I'm not fully giving up yet.

Any help, any clue, would be very appreciated!

I have experienced this issue as well on first launch after installing from the App Store or Testflight. For me this only occurs on first install, not on updates.

So far I haven't found a way to work around it without making the login/join forms work without cookies, which is probably a bigger pain than it's worth.

Same here, it's only the first launch after the first installation. When it's updated, it doesn't have the problem (unless there are some specific changes perhaps, I'm not sure).

Login/Join forms not working on the first launch after the first install is a major blocker for me. :(

Anyway, thank you @bezzer for reassuring me (and my teammates) on the fact that we're not the only one with this issue! (I have not found anything via Google on this particular matter so far.)

This bug has been there for more than a year in https://bugs.webkit.org/show_bug.cgi?id=140191

I finally gave up on trying to fix this bug. I'm now using a work-around, which consists in using localStorage to store those cookie stuff.

Realise this may be a bit late @pwbs , but I finally got a chance to look into this more closely, and was able to force the cookies to be stored by copying them from the server response into NSHTTPCookieStorage in the WKNavigationDelegate decidePolicyForNavigationResponse implementation.

NSHTTPURLResponse * resp = (NSHTTPURLResponse*)[navigationResponse response];
NSDictionary  *diction = [resp allHeaderFields];
NSArray * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:diction forURL:[resp URL]];
for (NSHTTPCookie *cookie in cookies) {
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
return decisionHandler(YES);

Cookies work on first install after this change for me, but it feels a bit heavy handed to copy them on every response.

@bezzer Thanks for your help. But unfortunately I couldn't make it work on my end.
In fact I'm using another wkwebview as well (cordova-plugin-wkwebview-engine).
I tried adding your code but did not work. Cookies is still not set at first launch after clean install.

@bezzer @chanphillip decidePolicyForNavigationResponse isn't called for ajax responses, so it doesn't work for me. Doesn't seem to apply to in-app browser responses either. The only time it's ever called in my app is for the cordova root file. e.g. file:///var/containers/Bundle/Application/XXX-XXX-XXX/MyApp.app/www/index.html
My root file uses XHRs to authenticate, and those XHRs don't store cookies properly unless the app is backgrounded and then foregrounded, even with @bezzer's fix. So, still not a fix for me.

@hobzcalvin does this workaround stop working if the cookies on the server change? I'm thinking of applying your hack but I'm worried that the client side cookies that are synced up the first time will eventually becomes stale.

Does anyone know why the heck this only happens on the first install? Is this related to Apple's attempts to thwart cookie tracking?