Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is SFSafariWebViewController not sharing cookies with Safari properly?

I created an incredibly basic app which includes a SFSafariViewController pointing at the URL http://www.w3schools.com/js/js_cookies.asp . This is a test website for reading and writing cookies.

I then loaded the same website into Mobile Safari, and added one cookie. I switched to my app, read the cookie, it's there. I go back to Safari, add another cookie, go back to my app, but the second cookie hasn't appeared. I refresh the pages, no difference. Go back to Safari and read the cookies, they are both read successfully.

Is there anything I need to do between apps in order for the cookies to be written and read properly?

like image 993
jowie Avatar asked Mar 24 '16 15:03

jowie


People also ask

Does SFSafariViewController share cookies with Safari?

SFSafariViewController no longer shares cookies with the standalone Safari browser. Thus, a website can no longer determine that the user on SFSafariViewController is the same user on Safari, even though the view controller and the browser are open on the same device.

Should cookies be blocked on Safari?

There is no harm in blocking the cookies from the website. However, on the sites where you sign in to an account like Instagram, Twitter, or Google the cookie should be enabled else your session won't last after closing the browser. For that one should know how to allow cookies on the iPhone or iPad.

How do I manually add cookies to Safari?

SAFARI for iOS (iPhone and iPad)Step 1: Go to Settings, then scroll down and select “Safari”. Step 2: Scroll down to “Privacy & Security”. Step 3: Verify “Block All Cookies” is ticked (green/white), click to allow cookies. Step 4: Clear the browser cache and reopen the browser.


2 Answers

A user on the Apple Dev Forums suggested that it might only work for "persisted cookies" not "session cookies". I wasn't setting an expiry date on my cookies. I changed that by getting a time in the future:

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

And then setting it in the header:

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

And now the cookie value appears in SFSafariViewController.

like image 92
Mr Speaker Avatar answered Nov 23 '22 09:11

Mr Speaker


You could enforce the SFSarfariViewController to dismiss when the app closes. This would ensure the webpage gets refreshed along with any new cookies.

In ViewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldDismiss:) name:UIApplicationDidEnterBackgroundNotification object:nil];

then:

- (void)shouldDismiss:(NSNotification*)notification {

    [self.safariViewContollerName dismissViewControllerAnimated:YES completion:nil]  
}

Hope this helps,

Liam

like image 39
Liam Ferris Avatar answered Nov 23 '22 08:11

Liam Ferris