【问题标题】:User Defaults Swift用户默认 Swift
【发布时间】:2020-05-13 11:49:01
【问题描述】:

我有提示视图(工具提示)。我希望每个下载应用程序在我的应用程序中显示 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)
        }
    }

如何控制用户何时删除应用并重新下载

【问题讨论】:

  • 您可以存储一个类似firstRunFinished 的布尔值,并在应用启动后设置为true。或者,如果您希望用户至少看到一次 ToolTip,那么您必须为此设置特定的布尔值。
  • 作为仅供参考,我会推荐 github.com/jrendel/SwiftKeychainWrapper 或来自 github.com/jrendel/SwiftKeychainWrapper 的东西。这些通常更可靠并且具有更高的安全性。不过,下面的答案绰绰有余。
  • @Аба-Бакри Ибрагимов 请检查我的答案,让我知道它是否有效。
  • 伙计们我有一个问题,我在两个不同的屏幕上有两个提示视图,我不能在两个屏幕上使用相同的键,但是当我创建两个键时,一个会影响另一个并改变他的bool 类型,如何避免它

标签: ios swift mobile nsuserdefaults userdefaults


【解决方案1】:

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")

【讨论】:

    【解决方案2】:

    更改 hintView 的 getter 和 setter,如下所示

    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")
    }
    

    希望你更清楚

    【讨论】:

      【解决方案3】:
      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)
             }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-11
        相关资源
        最近更新 更多