我想在用户第一次打开我的应用程序时显示一个欢迎屏幕。有什么方法可以检查Swift中应用程序的首次启动?
你可以在任何地方使用它来验证用户是第一次看到这个视图。
func isAppAlreadyLaunchedOnce() -> Bool {
let defaults = UserDefaults.standard
if let _ = defaults.string(forKey: "isAppAlreadyLaunchedOnce") {
print("App already launched")
return true
} else {
defaults.set(true, forKey: "isAppAlreadyLaunchedOnce")
print("App launched first time")
return false
}
}
注意:此方法会在用户重新安装应用程序并首次启动后返回
false
func isAppAlreadyLaunchedOnce()->Bool{
let defaults = NSUserDefaults.standardUserDefaults()
if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
println("App already launched")
return true
}else{
defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
println("App launched first time")
return false
}
}
由于应用程序的 NSUserDefaults 在卸载应用程序时被删除,您可以尝试在应用程序启动时测试是否存在某个值。
如果该值存在,则该应用程序已安装。如果不是,这是应用程序第一次启动,您设置该值。
斯威夫特:
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore {
print("Not first launch.")
} else {
print("First launch, setting UserDefault.")
UserDefaults.standard.set(true, forKey: "launchedBefore")
}
目标 - C :
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAppAlreadyLaunchedOnce"])
{
return true;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAppAlreadyLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
return false;
}
我经常发现
userDefault
由于一些 bug 苹果 data protection 在我的应用程序中的政策,所以我改变了我的策略来检测以下首次启动
if ( fileExists( dummyFilePath ) == NO ) {
createFileAt( dummyFilePath )
// This is first launch
}
斯威夫特 5
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore {
// Code if has launched before
} else {
UserDefaults.standard.set(true, forKey: "launchedBefore")
// Code if not has launched before
}
Objective-C 版本的Jon Shier 的回答:
BOOL isAppLaunched = [[NSUserDefaults standardUserDefaults] boolForKey:@"launchedBefore"];
if (isAppLaunched) {
NSLog(@"Not first launch.");
}
else {
NSLog(@"First launch, setting NSUserDefault.");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"launchedBefore"];
}
对 Jonas Deichelmann 的回答进行轻微的语义修改,澄清一些事情:
func establishUserDefaultsHaveBeenVerifed()->Bool{
let defaults = UserDefaults.standard
if let _ = defaults.string(forKey: "userDefaultsHaveBeenVerified"){
print("user defaults were already verified")
return true
}else{
defaults.set(true, forKey: "userDefaultsHaveBeenVerified")
print("verified user defaults for first time since app was installed")
return false
}
}
NOT 第一次会议时做某事(例如,显示汇率应用程序):
@UserDefault(key: UDKey.isFirstSession, defaultValue: true)
static var isFirstSession: Bool
func applicationWillTerminate(_ application: UIApplication) {
// When app is terminated, next sessions are not first anymore
UserDefaults.isFirstSession = false
}
什么是@UserDefault:
@propertyWrapper
struct UserDefault<Value> {
let key: String
let defaultValue: Value
var container: UserDefaults = .standard
var wrappedValue: Value {
get {
return container.object(forKey: key) as? Value ?? defaultValue
}
set {
container.set(newValue, forKey: key)
}
}
}
UDKey是什么:
enum UDKey {
static let isFirstSession = "isFirstSession"
}