Giter Site home page Giter Site logo

automatt / safariautologintest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mackuba/safariautologintest

0.0 1.0 0.0 161 KB

A demo showing how you can auto-login users to an iOS app (on iOS 9) based on Safari cookies

Swift 79.59% HTML 20.41%

safariautologintest's Introduction

Safari auto-login (iOS 9)

This demo shows how you can automatically log in users to your iOS app after they install it based on cookies previously set in Safari. The idea is that if you have a way of logging them in somewhere in the browser before they download your app, or they have previously logged in to your webapp before installing it, you can automatically recognize them without them having to go through an in-app login flow and enter the same login info again.

This project was inspired by this article by LaunchKit.

See it in action

Here's how it looks:

video

How it's done

It's actually pretty simple:

  • the app creates an SFSafariViewController at startup and tells it to load a special page in your webapp that automatically redirects back to your app using a custom URL scheme (that's the only way you can pass something back to your app, since the app has no direct access to the contents of the Safari View Controller)
  • the main view controller presents the Safari View Controller as a popup (it needs to be displayed to start loading the page), but to hide the Safari View Controller from the user, it sets its modalPresentationStyle to .OverCurrentContext and its view's alpha to 0
    • let me know if you know a better way to do this :)
  • the page reads the name from the previously set cookie and redirects to svclogintest://name/yournamehere; that triggers the callback application(handleOpenURL:) in the application delegate, which notifies the view controller about it through an NSNotification
  • when the page finishes loading, the view controller automatically cleans up the SVC
  • when the view controller receives the notification from the delegate, it updates the label in the center with the user's name

So this way you can remember the user between app installations and even before it's installed for the first time, as long as you control the site which sets the cookies and can make it redirect to the custom URL, and as long as the user doesn't clear the cookies in Safari. (I've seen an issue though where even after clearing the cookies the SVC was still seeing them, even though Safari didn't - might be a bug in the beta?)

How to test it

  • download or clone the project

  • in a terminal, go into the project's directory and start an HTTP server in that folder:

      python -m SimpleHTTPServer
    
  • open the project in Xcode 7

  • build & run

  • to log in & out, go to http://localhost:8000 in Safari in the simulator and use the form to update the name

Credits / license

Created by Kuba Suder, licensed under WTFPL license.

If you have any suggestions or comments, please let me know via Twitter @kuba_suder, email or GitHub issues.

Note: please don't use this for any evil purposes ๐Ÿ™

safariautologintest's People

Contributors

mackuba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.