1

SFSafariViewController allows a webpage to access cookie stored in Safari. I would like to perform some background task with SFSafariViewController to utilize the cookie. However I do not want the user to see the SFSafariViewController.

Is there a way to hide SFSafariViewController and allow user to touch the screen when SFSafariViewController is presented?

Duolan
  • 11
  • 4

2 Answers2

2
Class SFSafariViewControllerClass = NSClassFromString(@"SFSafariViewController");
if (SFSafariViewControllerClass) {
    id safController = [[SFSafariViewControllerClass alloc] initWithURL:[NSURL URLWithString:urlString]];

    UIViewController *windowRootController = [[UIViewController alloc] init];

    self.secondWindow = [[UIWindow alloc] initWithFrame:CGRectZero];
    self.secondWindow.rootViewController = windowRootController;
    [self.secondWindow makeKeyAndVisible];
    [self.secondWindow setAlpha:0];

    [windowRootController presentViewController:safController animated:YES completion:^{
        [self.secondWindow.rootViewController dismissViewControllerAnimated:NO completion:NULL];
    }];
}

you can use the code to hide the SFSafariViewControllerClass

Away Lin
  • 91
  • 1
  • 4
  • 1
    setting the alpha to zero no longer works in iOS 10. You can set the main window.windowLevel to a high number, then set it back to 0. – Brian Aug 29 '16 at 23:29
  • @Away Lin did u face any rejection of your app while submitting? I am asking this since the usage of SFSafariViewControllerClass in the answer will violate the legal point as pointed out in the second answer – vignesh kumar Dec 13 '17 at 06:24
2

Don't do this if you're planning to submit to the App Store.

From https://developer.apple.com/app-store/review/guidelines/:

5.1.1 (iv) SafariViewContoller must be used to visibly present information to users; the controller may not be hidden or obscured by other views or layers. Additionally, an app may not use SafariViewController to track users without their knowledge and consent.

If you need to access cookies, use WKWebView instead. See Getting all cookies from WKWebView. These cookies aren't shared with Safari, though.

Community
  • 1
  • 1
jrc
  • 20,354
  • 10
  • 69
  • 64