Reputation: 9269
I'm developing and iOS app that has a WKWebView
and a native login screen. On the native login screen the user can login which is done with an API call that returns a JSON object and two cookies.
What I'm trying to do is that when the call was a success and the user goes to the WKWebView
screen, they are logged in on the website that is shown as well.
The WKWebView
is already loaded so my question is how should I handle the cookies so that the user is logged in on the WKWebView
?
I've read this question & answer but that doesn't work for me. The WKWebView
isn't showing anything so I think it is blocked or in a loop.
UPDATE I'm using multiple WKWebViews, I found out that if I do the API call, then go to one WKWebView it's working fine, I'm logged in. Probably the tricky part here is that I'm using multiple WKWebViews.
Upvotes: 1
Views: 3117
Reputation: 9269
I finally found the solution for my cookie problem.
I had to create one global WKProcessPool
for all my WKWebViews
and I had to send the cookies from HTTPCookieStorage.shared.cookies
with every login call I did so the cookies I got back where still connected to the WKWebViews
and nothing was lost. Hope this helps somebody with the same problem.
Upvotes: 3
Reputation: 39
Your use case is supported in iOS11 in an elegant way with WKHTTPCookieStore. After login and getting the cookie via the JSON api, use setCookie to automatically login the user in the WKWebView.
WKWebView is receiving a major update in iOS11 and opening up many new interesting possibilities. There is a demo of your specific use case in the Customized Loading in WKWebView WWDC session.
Unfortunately this will not help if you need to get this done for iOS10 or earlier.
Upvotes: 2