8

I'm not sure if it's possible. I'm currently developing an app the stores password (like a password manager). One of its features is to allow user to make an auto login to a web browser of that device. I was able to make it work using UIWebView and WKWebView but can't find a way to make it work using SFSafariViewController. I read few documentations and blogs about it and even read the SFSafariViewController.h but there's no clue in there that can lead me to the function that I wanted. I want to know how to do a "evaluateJavaScript"-a-like function in SFSafariViewController or if its not possible, do you guys have other suggestions on how to achieve this?

Note: I'm using Objective-C.

Thanks in advance! Happy coding!

mCube
  • 334
  • 3
  • 8
  • I don’t think this is possible — `SFSafariViewController` Apple doesn’t allow interaction: “The user's activity and interaction with SFSafariViewController are not visible to your app” [(docs)](https://developer.apple.com/library/prerelease/ios/documentation/SafariServices/Reference/SFSafariViewController_Ref/index.html). – Jed Fox Jan 26 '16 at 22:59
  • Is there other way to implement auto login from app to a specified URL? I'm thinking of putting up a web API to my server and do the trick there and post it to the login URL of the site where I wanted to login. Is that possible? – mCube Feb 04 '16 at 14:22
  • 2
    You should just use `UIWebView` or `WKWebView`. If you want to log in with a system view, you can use [`AFNetworking`](https://github.com/AFNetworking/AFNetworking) to send a POST request to your server. – Jed Fox Feb 04 '16 at 16:18
  • Yup, I tried that and even made that work. But my client wanted it to open Safari after logging in. Is it possible? – mCube Feb 04 '16 at 19:05
  • 1
    Yes! Just call `[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"your URL here"]]`. – Jed Fox Feb 04 '16 at 19:08
  • That will call the Safari. But the authenticated session won't be passed right? I mean if I requested sample.com as openURL it will be treated as different instance and will still ask for the user's credentials – mCube Feb 04 '16 at 19:10
  • You could (for example) pass an authentication token in the URL that would transfer the session, then be invalidated. – Jed Fox Feb 04 '16 at 19:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102632/discussion-between-mcube-and-j-f). – mCube Feb 04 '16 at 19:15

1 Answers1

3

SFSafariViewControllers are sandboxed. As they are essentially Safari, and have access to cookies. It would be a security risk to allow any app to access these.

However, if the web-service you want the user to sign in to is yours, you can setup a API and a custom URL scheme.

Follow this documentation.

If you are trying to store passwords etc from sites you don't maintain then unfortunately, this is not possible using the SFSafariViewController.

Hope this helps,

Liam

Liam Ferris
  • 1,786
  • 2
  • 18
  • 38