Why is SFSafariWebViewController not sharing cookies with Safari properly?

爱⌒轻易说出口 提交于 2021-02-17 21:58:26

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/36203617/why-is-sfsafariwebviewcontroller-not-sharing-cookies-with-safari-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!