You are currently viewing How Anonymous Mode is Implemented in SUSI iOS
How Anonymous Mode is Implemented in SUSI iOS

How Anonymous Mode is Implemented in SUSI iOS

Anonymous Login is an easy way for people to try an app without sharing any of their personal information. Sometimes people want to try out apps, but they’re not ready to share any information about themselves. For this, SUSI introduced a way to log in to apps anonymously.

In SUSI iOS we use login and signup to save user’s messages, preferences, settings and secure messages from others. There are Login and Sign up features presents in SUSI iOS app. But the user can also use the app without logging in. Here I will show you, how the anonymous mode implemented in SUSI iOS.

The user can use app either by login using username and password or anonymously by clicking the skip button.

At the time of the login process, we store the user’s data in User object with `accessToken` and during `signout` process we remove the user key from `UserDefaults`. User Defaults is an interface to the user’s defaults database, where user store key-value pairs persistently across launches of the app.

When the user enters in anonymous mode by clicking the Skip button, `currentUser: user` object set to the nil.

Here is how the skip button action implemented in LoginViewController:

  • Delete the current Realm database from the device
  • Delete the trained voice model from the device
  • Reset all settings to default settings
  • Set SUSI API URL as server URL
  • Enter into chat screen
@objc func enterAnonymousMode() {
resetDB()
deleteVoiceModel()
resetSettings()
UserDefaults.standard.set(Client.APIURLs.SusiAPI, forKey: ControllerConstants.UserDefaultsKeys.ipAddress)
completeLogin()
}

When the user enters into the Login screen, we check the existing session by `checkSession()` method:

func checkSession() {
if let userDefaultValue = UserDefaults.standard.value(forKey: ControllerConstants.UserDefaultsKeys.user) {
if let userData = userDefaultValue as? [String: AnyObject] {
let user = User(dictionary: userData)
saveUserGlobally(user: user)

DispatchQueue.main.async {
if user.expiryTime > Date() {
self.completeLogin(false)
self.fetchUserSettings(user.accessToken)
} else {
self.resetDB()
    }
   }
  }
 }
}

Save user globally so that it can be checked anytime whether the user is logged in or entered anonymously by `currentUser` value nil or not. In the case of anonymous mode, `currentUser` is nil.

func saveUserGlobally(user: User) {
 if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  appDelegate.currentUser = user
 }
}

The user can always login into the app. If the user is using the app in anonymous mode but he/she want to login then he/she can log in. There is an option for login in Settings Menu.

Clicking on login button, direct user to Login screen where the user can log in with registered email and password.

Resources –

  1. User Defaults: https://developer.apple.com/documentation/foundation/userdefaults
  2. App Delegate: https://developer.apple.com/documentation/uikit/uiapplicationdelegate?hl=et
  3. SUSI iOS Link: https://github.com/fossasia/susi_iOS

Jogendra Kumar

Jogendra is pre-final year student at Indian Institute of Technology (BHU) Varanasi. He is an iOS Developer and an active Open Source contributor, also have experience in developing Android, Web and Windows Apps. Currently, he is focusing on Machine Learning and Blockchain Technology. Apart from Coding, he loves coffee, going hackathons, attending conferences, and traveling. You can find him on twitter here https://twitter.com/imjog24

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.