SFSafariViewController Cookies

I understand that with iOS9 you can read cookies using 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 this:

 - (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully { NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; NSArray *cookiesArray = [storage cookies]; } 

cookieArray is always empty.

If I use the traditional UIWebView interface

 -(void)webViewDidFinishLoad:(UIWebView *)webView { NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; NSArray *cookiesArray = [storage cookies]; } 

I get the cookie I was expecting.

Any ideas what I can do wrong?

+6
source share
1 answer

SFSafariViewController is basically a Safari process that runs outside of your application. Your application will not have access to cookies used by SFSafariViewController , just as your application will not have access to cookies in Safari itself.

If you need this functionality, you need to get stuck with UIWebView or WKWebView .

+7
source

Source: https://habr.com/ru/post/1013294/


All Articles