9

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?

jowie
  • 8,028
  • 8
  • 55
  • 94
  • 1
    which are the properties of the cookies ? look inside NSHTTPCookieStorage – atrebbi Apr 05 '16 at 05:52
  • Is this affected by SFSafariViewController? It's worth an investigation so thanks. – jowie Apr 05 '16 at 15:37
  • 2
    Hey, did you ever figure this out? I am trying out a Safari auth method into my app and am having the same issue. Browser requests show cookies just fine, but in-app SFSafariVC requests don't. – Dima May 12 '16 at 00:13
  • Are you really reading cookies from Safari app to inside your app using SFSafariViewController? I need to read cookies from Safari app to inside my app. Is it possible? – Ganesh G Nov 18 '16 at 10:27

3 Answers3

5

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.

Mr Speaker
  • 3,047
  • 24
  • 17
4

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

Liam Ferris
  • 1,786
  • 2
  • 18
  • 38
  • Awesome find, what if your use case is the other way around your app isn't able to get the latest cookie from Safari? – Mills Apr 06 '16 at 10:35
  • That's a nice "hack" but unfortunately it doesn't help our specific use case. We are coming from Safari, storing the cookie there, and then when the app is launched we are using SFSafariViewController to try to read that cookie. Unfortunately when we try to read it, sometimes it doesn't read as being set. – jowie Apr 06 '16 at 10:41
2

As Apple's document said:

The SFSafariViewController class provides a standard interface for browsing the web. The view controller includes Safari features such as Reader, AutoFill, Fraudulent Website Detection, and content blocking. It shares cookies and other website data with Safari. The user's activity and interaction with SFSafariViewController are not visible to your app, which cannot access AutoFill data, browsing history, or website data. You do not need to secure data between your app and Safari.

By default it's shares cookies and other website data with Safari. You don't have to do anything.

Tiep Vu Van
  • 975
  • 8
  • 13
  • okay yeah I'd read that before. Thanks for sharing. Cookies are being shared about 50% of the time, and usually only refresh if you force-quit and reload the app. – jowie Apr 05 '16 at 08:33
  • I need to read cookies from Safari app to inside my app. Is it possible? – Ganesh G Nov 18 '16 at 10:28