User Defaults Swift

I have hint view (tooltip). And I want it display in my app 1 time per download app. When user downloading app this tooltip is showing and then dismiss. When user delete app and again downloading tooltip should work again.

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)

and I have key

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

How can I control when user deletes app and again download it

Upvotes: 2

Views: 130

Answers (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)
       }
}

Upvotes: 0

HardikS
HardikS

Reputation: 714

Change your getter and setter for hintView like below

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

And now use your hintView variable like below for showing and hiding the toolbar.

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

I hope it will more clear to you

Upvotes: 1

Keshu R.
Keshu R.

Reputation: 5215

Store a bool in UserDefaults. Once the user uninstalls the app, the data will be deleted.

in your 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.

then inside your didFinishLaunchingWithOptions function

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

Upvotes: 2

Related Questions