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.
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:
- Select your scheme and click
Edit Scheme...
or useProduct ➞ Scheme ➞ Edit Scheme...
- Go to
Run
and open theArguments
tab - Add a new value to
Arguments Passed On Launch
- In this example, add:
-hasSeenAppIntroduction 0
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.
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.
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