Search code examples
ioscookiessafarisfsafariviewcontrollernshttpcookiestorage

SFSafariViewController cookies


I understand that as of iOS9 you should be able to read cookies with 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 :

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

cookiesArray is always empty.

If I use a traditional UIWebView

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

I get the cookie I was expecting.

Any ideas what I might be doing wrong?


Solution

  • SFSafariViewController is basically a Safari process, running outside of your app. Your app will not have any access to the cookies used by the SFSafariViewController, just as your app has no access to the cookies in the Safari app itself.

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