41

I create a few WkWebViews in my app, but when cookies are set for one webview, they do not take effect in the other ones (i.e. the other webviews do not pass the same cookie back to the server). How can I get them to all use the same cookie storage?

weiyin
  • 6,819
  • 4
  • 47
  • 58

1 Answers1

73

Got this working by using the same WKProcessPool for all the webviews.

First create a process pool once somewhere:

processPool = [[WKProcessPool alloc] init];

Then use it when creating WKWebviews. The pool must be set in the init method, not afterwards.

WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.processPool = processPool;
webview = [[WKWebView alloc] initWithFrame:frame configuration:config];
weiyin
  • 6,819
  • 4
  • 47
  • 58
  • 2
    Thanks. It did work in my case where i have hybrid app. Login webpage stores cookies in singleton processpool and later on in other page it will pickup cookies (sessionid, authenticationdata) from same singleton processpool. – harshit2811 Jul 30 '15 at 09:35
  • Do you know, will it anyhow affect performance of WKWebView if you're loading two at the same time? – Balki Sep 08 '16 at 15:55
  • 5
    on iOS10, by default, cookies are shared between multiple WKWebView. I have verified this on iOS10 simulator, but not sure whether it's true or not on iOS8/9 – ikzjfr0 Jan 11 '17 at 10:33
  • @ikzjfr0 are you sure about what you're saying? I tried today on iOS 10 and I need the shared pool instance if I want to keeps cookies shared between wkwebview. – Fabrizio Duroni Mar 10 '17 at 11:03
  • @FabrizioDuroni i'm serious, at least it's true on simulators – ikzjfr0 Mar 14 '17 at 04:47
  • 1
    I've seen the same - don't need WKProcessPool in simulator, but need it on device. – Miha Markic Mar 08 '18 at 10:27
  • 4
    how about if user restart app , in this case processpool object initialize again then how cookies sync ? – Muhammad Shauket Nov 01 '18 at 01:57
  • Today in iOS 13+ this issue is still hanging around. Does anybody has a solution that persist? – Raditya Kurnianto Mar 18 '20 at 00:18