Search code examples
iosswiftsafarisfsafariviewcontrollersfauthenticationsession

Sync with Safari - extension for SFSafariViewController


I know that SFSafariViewController has private cookie storage and other implementations. However, I know that we may write an extension for every (every native system) Swift class. I need either to share cookies with native Safari, or to open Safari from my app with some header request. At least, how to create an API for getting/setting cookies?


Solution

    1. Cookie sharing between SFSafariVC and native Safari has been disabled in iOS >= 11.

    At WWDC, Apple announced a change in the behavior of Safari View Controller with iOS 11 in that stored data (cookies, local storage, etc.) will no longer be shared between instances of Safari View Controller. We've confirmed this in-house with a beta version, and this seems to be independently confirmed by the folks at Branch.

    From AppAuth-iOS' repo: user whitehatguy

    1. The only API for SFSafariVC is to create a new instance for a given URL. You can't set or modify cookies.

    If you need to execute custom code, have a look at WKWebView (iOS >= 8) / UIWebView (iOS < 8). Useful answers: Getting cookies in WKWebView

    SFAuthenticationSession was created to allow SSO/OAuth authentication flows, which shares cookies with native Safari but developers have no direct access.

    A class that manages sharing a one-time web service login, along with cookies and website data, between Safari and an app, which can also be used for automatic login for other associated apps.

    The two cases where you would use SFAuthenticationSession are:

    • Logging in to a third party's service using an authentication protocol (e.g. OAuth). This option works well for social network applications.
    • Providing a single sign-on (SSO) experience for applications. This option works well for enterprise companies that have many applications installed on the same device.

    SFAuthenticationSession's documentation by Apple