用户默认值Swift

时间:2020-01-28 04:59:55

标签: ios swift mobile nsuserdefaults userdefaults

我有提示视图(工具提示)。而且我希望每个下载应用程序在我的应用程序中显示1次。用户下载应用程序时,将显示此工具提示,然后将其关闭。当用户删除应用并再次下载工具提示时,应该可以再次使用。

let options: AMTooltipViewOptions = .init(textColor: Color.guideSubTitle,
                                                  textBoxBackgroundColor: Color.guideScreenBackground,
                                                  textBoxCornerRadius: 8,
                                                  lineColor: Color.guideScreenBackground,
                                                  lineHeight: 15,
                                                  dotSize: 0,
                                                  focusViewRadius: 15,
                                                  focustViewVerticalPadding: 0,
                                                  focustViewHorizontalPadding: 0)
        AMTooltipView(options: options,
                      message: Localizable.scan_open_from_gallery + "\n" + Localizable.scan_clear,
                      focusView: content.openGalleryBtn, target: self)

我有钥匙

public var hintView: Bool {
        get {
            return setting.bool(forKey: Key.hintView)
        }
        set {
            setting.set(false, forKey: Key.hintView)
        }
    }

如何控制用户何时删除应用并再次下载

3 个答案:

答案 0 :(得分:2)

将布尔值存储在UserDefaults中。用户卸载应用后,数据将被删除。

在您的 AppDelegate.swift

let DEFAULTS = UserDefaults.standard
var isUserFirstTime = !DEFAULTS.bool(forKey: "isUserFirstLogin") // by default it will store false, so when the user opens the app for first time, isUserFirstTime = true.

然后在您的didFinishLaunchingWithOptions函数中

 if isUserFirstTime {
     // your code here to show toolbar
        } else {
            // dont show toolbar
        }
  // once you have completed the operation, set the key to true. 
  DEFAULTS.set(true, forKey: "isUserFirstLogin")

答案 1 :(得分:1)

如下所示更改hintView的吸气剂和吸气剂

public var hintView: Bool {
    get {
        return setting.bool(forKey: Key.hintView)
    }
    set {
        setting.set(true, forKey: Key.hintView)
        setting.synchronize()
    }
}

现在使用如下所示的hintView变量来显示和隐藏工具栏。

//it will always returns false for first time when you install new app.
if hintView {
   print("Hide Toolbar")
}
else {
   //set flag to true for first time install application.
   hintView = true
   print("Show Toolbar")
}

希望您会更清楚

答案 2 :(得分:0)

import Foundation
import AMTooltip

class HintViewController {

    let userDefaults: UserDefaults = .standard

    let wasLaunchedBefore: Bool

    var isFirstLaunch: Bool {
        return !wasLaunchedBefore
    }

    init() {
        let key = "wasLaunchBefore"
        let wasLaunchedBefore = userDefaults.bool(forKey: key)
        self.wasLaunchedBefore = wasLaunchedBefore
        if !wasLaunchedBefore {
            userDefaults.set(true, forKey: key)
        }

    }

     func showHintView(message: String!, focusView: UIView, target: UIViewController) {

        let options: AMTooltipViewOptions = .init(textColor: Color.guideSubTitle,
                                            textBoxBackgroundColor: Color.guideScreenBackground,
                                            textBoxCornerRadius: 8,
                                            lineColor: Color.guideScreenBackground,
                                            lineHeight: 15,
                                            dotSize: 0,
                                            focusViewRadius: 15,
                                            focustViewVerticalPadding: 0,
                                            focustViewHorizontalPadding: 0)

           AMTooltipView(options: options, message: message, focusView: focusView, target: target)
       }
}
相关问题