Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 878
Jai Byron Avatar asked Dec 19 '16 15:12

Jai Byron


People also ask

Does SFSafariViewController share cookies?

Cookies. In iOS9, cookies between the Safari system browser and the SFSafariViewController are only shared if they are persistent. Session cookies are not shared and can result in different authentication sessions between the system browser and the Safari view controller.

What is SFSafariViewController?

SFSafariViewController is an object that provides a visible standard interface for browsing the web. The view controller includes Safari features such as Reader, AutoFill, Fraudulent Website Detection, and content blocking.

Does WKWebView share cookies with Safari?

You can share cookies between multiple WKWebView 's inside your app by utilising WKProcessPool . There is a way of passing cookie data from Safari to your app by combining SFSafariViewController (iOS 8 and below you will need to switch to Safari) with a custom URL scheme.


1 Answers

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.

like image 169
wottle Avatar answered Sep 21 '22 17:09

wottle