Overriding UserDefaults for improved productivity – onlinecode

Overriding UserDefaults for improved productivity – onlinecode

In this post we will give you information about Overriding UserDefaults for improved productivity – onlinecode. Hear we will give you detail about Overriding UserDefaults for improved productivity – onlinecodeAnd how to use it also give you demo for it if it is necessary.

UserDefaults within apps are used to store data related to user configurations. It’s an easily accessible data store for saving states, statistics, and other app-related data.

Launch arguments are passed to the application on launch and can be used to alter the application for debugging purposes. It’s often used by Apple to allow developers to enable specific debugging modes, like in Core Data. The same we can do ourselves to enable certain debug modes in our apps.

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

An example case: UserDefaults to trigger an app introduction

A common use-case for using launch arguments is to force enable an introduction screen during development. Without this, we would need to reinstall the app every time so that the introduction screen shows up.

An example code implementation could look as follows:

var shouldShowIntroduction: Bool {
    /// Use a launch argument to always skip the app introduction.
    if ProcessInfo.processInfo.arguments.contains("-skipAppIntroduction") {
        return false
    }

    /// Use a launch argument to always show the app introduction.
    if ProcessInfo.processInfo.arguments.contains("-requireAppIntroduction") {
        return true
    }

    /// No launch arguments found, read from the user defaults.
    return UserDefaults.standard.bool(forKey: "hasSeenAppIntroduction") == false
}

In this case, we have three options:

  • Force-skip the introduction using the -skipAppIntroduction launch argument
  • Force-enable the introduction using the -requireAppIntroduction launch argument
  • Read the value from the user defaults

Although this works, it will easily fill up your launch arguments and it requires extra code with potential bugs. It would be much easier to simply override the user defaults directly. If so, we could have a simple code implementation like:

import SwiftUI

@main
struct UserDefaultsLaunchArgumentsApp: App {
    var body: some Scene {
        WindowGroup {
            if UserDefaults.standard.bool(forKey: "hasSeenAppIntroduction") == false {
                IntroductionView()
            } else {
                ContentView()
            }
        }
    }
}

Using launch arguments to override user defaults

Overriding user defaults using launch arguments can be done in the scheme editor. In this case, we’d like to force-enable the introduction screen by overriding the hasSeenAppIntroduction user defaults value.

You can do this by following these steps:

  1. Select your scheme and click Edit Scheme... or use Product ➞ Scheme ➞ Edit Scheme...
  2. Go to Run and open the Arguments tab
  3. Add a new value to Arguments Passed On Launch
  4. In this example, add: -hasSeenAppIntroduction 0
Overriding user defaults using launch arguments

Launch arguments need to be passed using the dash sign in front of the key. The value can be added after the key. In this case, we will use a zero which equals to false for booleans. We could’ve used a value of 1 if we would like to force disable the introduction screens.

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

Conclusion

And that’s it! We’ve enabled our app introduction for development while not touching our code. A custom launch argument allows us to alter the value of the user defaults so that the introduction is always shown.

See also 

Change default configuration name of Laravel's created_at and updated_at

In this post we will give you information about Change default configuration name of Laravel's created_at and updated_at. Hear we will give you detail about Change default configuration name of Laravel's created_at and updated_atAnd how to use it also give you demo for it if it is necessary.

In this Laravel PHP Tutorial, I will let you know the use of created_at and updated_at column in a database table.

By default, Eloquent automatically maintain the date time value in created_at and updated_at column on your database table. If you do not want for eloquent to maintain created_at and updated_at columns then disable it by adding following property in your model class :

  1. class Member extends Eloquent {
  2. protected $table='members';
  3. public $timestamps= false;
  4. }
class Member extends Eloquent {

 protected $table = 'members';

 public $timestamps = false;

}

If you want to map Laravel's timestamp from created_at to created_on and updated_at to modified_on then you can override const on your model in following way :

const CREATED_AT = 'created_on';
const UPDATED_AT = 'modified_on';

Now Eloquent will take care of the column "created_on" and "modified_on" on your database table.

How to disable created_at and updated_at timestamps in Laravel Model?

Try this..

Hope this code and post will helped you for implement Change default configuration name of Laravel's created_at and updated_at. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

If you like to improve your Xcode knowledge, even more, check out the Xcode category page. Feel free to contact me or tweet to me on Twitter if you have any additional tips or feedback.

Thanks!

 

Hope this code and post will helped you for implement Overriding UserDefaults for improved productivity – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US