SFSafariViewController does not send cookie set from Safari Browser session

Hi,


I have a scenario where a user opens a url in Safari browser, which then has a cookie set from the response. When my app opens, I am expecting the SFSafariViewController to be able send that cookie in a request to the same server that was visited in Safari.


When I try this scenario, the cookie is set in the Safari browser, but when my app launches and a SFSafariViewController is launched to the same server endpoint, no cookie is received on the server side.


Example sequence below:


1. User loads "http://localhost:1337" in Safari browser. Server responds with a cookie of "value=X; Path=/"

2. User loads app and app launches a SFSafariViewController with url of "http://localhost:1337/mypath"


I expected my server to receive the cookie "value=X" in the request "http://localhost:1337/mypath" made by the SFSafariViewController, but nothing is received on the server. If I try the same request from the Safari browser, then the cookie is received by the server.


Is this type of scenario viable with SFSafariViewController? From the docs it mentions that SFSafariViewController can access cookie from Safari browser so i expected this to work.


Any help or pointers are appreciated.

hi ,I have the same problem with yours. Have you got any solutions?

Do these by chance happen to be session cookies?


See my suspicion:

https://forums.developer.apple.com/message/105133#146880


Session cookies appear not to be shared (at least as of 9.3.2), but I can't find a solid confirmation besides my own tests and posts like these.

Tronvo, high-fives for you! I was going crazy with this. Setting an expiry in the future worked!


I made a time in the future:

const expireTime = new Date(Date.now() + 1000 * 60 * 60 * 24).toGMTString();


Then set the header:

"Set-Cookie":`query=${uri.query}; path=/; expires=${expireTime}`


Works! The cookie now shows up in SFSafariViewController - thanks again!

I understand that as of iOS9 you should be able to read cookies with SFSafariViewController.



If I set a cookie on my page in JS using the following:



var dd = new Date(Date.now() + 1000 * 60 * 60 * 24).toGMTString();

var expires = "expires="+ dd;

document.cookie = "mycookie=cookievalue; " + expires + " domain=.mydomain.co.uk ; path=/ ";



If I do :



- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully

{

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *cookiesArray = [storage cookies];

}



cookiesArray is always empty.



If I use a traditional UIWebView



-(void)webViewDidFinishLoad:(UIWebView *)webView

{

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *cookiesArray = [storage cookies];

}



I get the cookie I was expecting.



Any ideas what I might be doing wrong?

SFSafariViewController does not send cookie set from Safari Browser session
 
 
Q