In this iOS app I'm working on, there are two landing screens shown to the user depending on a particular status. The screens are Login screen and Activate screen.
Let me explain what this Activate screen is. There is something called activating users. It's done by a web backend and the app has no control over it. Each device the app runs has a unique device ID. This unique ID is generated in the device. A person manually activates this ID from the backend. And there are two ways this ID gets to the backend. Either it's sent from the device at first launch, or it can be obtained by some other method and entered to the backend database manually.
When the app launches, you must get that ID and send it over to the backend to check if it's activated or not. You get two statuses, 100 - Activated, 101 - Not Activated. I keep a boolean flag called DeviceIsActive
in NSUSerDefaults
to store the result.
When the app launches first it checks if the DeviceIsActive
is true, if it is, the user is directed to the Login screen. If its false, you're directed to the Activate screen where you're informed to contact the person at the backend to activate it.
Here is the code I have in the didFinishLaunchingWithOptions
in the AppDelegate file which is the method that runs first when the app is launched.
var window: UIWindow?
private enum Screen {
case ActivateScreen
case LoginScreen
}
let api = API()
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
if Preferences.getDeviceIsActive() == true {
// Checks if deviceIsActive flag is true.
redirectToView(.LoginScreen)
} else {
// If there is not deviceID, generate one.
if Preferences.getDeviceID() == nil {
var deviceID = NSUUID.UUID().UUIDString
deviceID = deviceID.stringByReplacingOccurrencesOfString("-", withString: "") // e,g: 70788AEB8586513C46B0599AEA4F82BF
Preferences.setDeviceID(deviceID)
// Send the ID to the backend to check.
register()
} else {
// If there is a deviceID already, still send it to the server to check and make sure.
register()
}
}
return true
}
private func register() {
api.registerDevice(Preferences.getDeviceID(), completionHandler: { (response, error) -> Void in
let (status, code) = response
if status == Status.DeviceIsActive.toRaw() { // Status is an enum. It has all the statuses (100, 101).
Preferences.setDeviceIsActive(true)
self.redirectToView(.LoginScreen)
} else {
self.redirectToView(.ActivateScreen)
}
})
}
private func redirectToView(screen: Screen) {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
switch screen {
case .ActivateScreen:
let activateVC: ActivateViewController = storyboard.instantiateViewControllerWithIdentifier("ActivateController") as ActivateViewController
let navigationVC = UINavigationController(rootViewController: activateVC)
self.window?.rootViewController = navigationVC
case .LoginScreen:
let loginVC: LoginViewController = storyboard.instantiateViewControllerWithIdentifier("LoginController") as LoginViewController
let navigationVC = UINavigationController(rootViewController: loginVC)
self.window?.rootViewController = navigationVC
default:
println("No view controller specified")
}
self.window?.makeKeyAndVisible()
}
Now the code works fine. But it's just so ugly and messy with all the if else
statements all over the place. Is there any way to make it cleaner?