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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With