Home > Software design >  Run code for existing users only - SwiftUI/iOS/Swift
Run code for existing users only - SwiftUI/iOS/Swift

Time:06-04

I have a production-ready app where I need to run some code only on users with the previous versions installed but not on new installations. For instance, if 1.0 is the latest version in the AppStore and 2.0 is the new one that will introduce code that needs to run only on users with version 1.0 but not on new users.

e.g.

if isExistingUser{
    // run code
}

What would be the best way to run some code only for existing users? How can one determine whether it's a new or existing user?

CodePudding user response:

I see this often and knowing how to do this can be extremely valuable, especially when doing something like introducing a new feature that changes the experience your previous users had.

There are a few approaches you can take depending on your needs.

Firstly, you could create a boolean variable in your user model class that is set during user registration in the standard user registration flow and indicates that this is a newly created user - and name it something like isNewOnVersionTwo which will indicate this user is a new user on this new version.

Code Example:

class User: Decodable { 
                
    var uid: string! 
    var username: string!  
    var isNewOnVersionTwo: Bool = false 
}
         
class RegistrationViewController: UIViewController { 
                 
    var user: User!  
    var isNewOnVersionTwo: Bool = false 
                       
    override func viewDidLoad() {
       super.viewDidLoad()
                  
       user.isNewOnVersionTwo = true              
    }
}

class HomeViewController: UIViewController {
            
   var user: User!
                
   override func viewDidLoad() {
      super.viewDidLoad()
            
      isNewOnVersionTwo == true ? normalInit() : showOldUserAView()
   }
            
   func normalInit() {

      // Run normal functions for the 'HomeViewController' or do nothing.
   }
            
   func showOldUserAView() {

       //Do something specific for only old users. 
    }
}

You can choose whether you want to hold onto this variable permanently or not - it could be useful for tracking the new users you've gained on this version versus the previous versions - and you could send it to your database along with the rest of the data from your user model.

A second and cleaner approach... Could be to only set the boolean on the very last view controller of the registration flow and pass it to the home view controller when you push the user to the view controller.

Like this:

class ViewControllerOne: UIViewController {

     var isNewOnVersionTwo: Bool = false 

     private func pushToNewViewController() {

        let vc = HomeViewController()
        vc.isNewOnVersionTwo = true 
        navigationController?.pushViewController(vc, animated: true)
     } 
}

class HomeViewController: UIViewController { 

    var isNewOnVersionTwo: Bool = false 

    override func viewDidLoad() {
       super.viewDidLoad()
                
       isNewOnVersionTwo == true ? normalInit() : showOldUserAView()
    }
                
    func normalInit() {

       // Run normal functions for the 'HomeViewController' or do nothing.
    }
                
    func showOldUserAView() {

       //Do something specific for only old users. 
     }
 }

I wouldn't take the approach of using UserDefaults for the few following reasons:

1. Devices occasionally wipe this randomly and it's not as reliable as hardcoding a flag in the source code.

2. UserDefaults isn't available for a few moments on cold app launches / initial startup which can complicate things, varying on when you want to show the older users whatever you want to show them.

CodePudding user response:

Does your app create any data? Maybe files in the Documents directory, or maybe a UserDefaults key? If so, check for the presence of one of those things very early in launch, which will signal to you that this must be an upgrade and you should do your thing.

A lot of people store the app's CFBundleShortVersionString Info.plist key into UserDefaults at launch, which makes it easy to know the last version that was run and let you write the logic of what needs to happen to migrate from that version to the new version. This is something you might want to think about doing in future versions.

  • Related