[SWIFT] A function that displays only once while Alert is running and never displays it again

Common choices in dialogs

Pattern ① OK --After pressing, it will not be displayed anymore during startup. Will be displayed again the next time it is started

Pattern ② Never display again --After pressing, it will not be displayed anymore. It will not be displayed even after restarting. If you uninstall the app and install it again, it will be displayed again.

I tried to implement each, so make a note of it.

Preparation for setting Flag to True and not displaying during startup after starting the application and pressing OK in the dialog

DialogHelper


class DialogHelper {
    
    static let shared = DialogHelper()
    var dialogDidSee = false
    
    func getDialogStatus()->Bool{
     
        return dialogDidSee
    }
    
    func setDialogStatus(_ flag:Bool){
        
        dialogDidSee = flag
    }
}

To prevent it from being displayed again until it is uninstalled Preparation of User Defaults to save data that will not be erased even after the application is closed and data of user settings

UserDefaultsKey


enum UserDefaultsKey: String {
    
    case dialogNoMore = "dialog_neverAgain"
    
    func initalValue<T>()-> T?{
        switch self{
        case .dialogNoMore:
            return false as? T
            
        default:
            return nil
        }
    }
}

Create Alert in ViewController like this Create two methods to determine if you should issue an Alert.

ViewController


    func showDialogIfNeed(){
        
        let didSee = DialogHelper.shared.getDialogStatus()
        let neverSee = UserDefaults.standard.bool(forKey: UserDefaultsKey.dialogNoMore.rawValue)
        
        if neverSee {
            return
        }else{
            if didSee {
                return
            }else {
                createDialog()
            }
        }
    }

    func createDialog(){
        
        let alert:UIAlertController = UIAlertController(title: "Here is a message for you!", message: "", preferredStyle: .alert)
        
        let okAction:UIAlertAction = UIAlertAction(title: "OK", style: .default, handler: {
            (action:UIAlertAction!) -> Void in
            DialogHelper.shared.setDialogStatus(true)
        })
        
        let neverAgainAction:UIAlertAction = UIAlertAction(title: "Never remind me", style: .default, handler: {
            (action:UIAlertAction!) -> Void in
            UserDefaults.standard.set(true, forKey: UserDefaultsKey.dialogNoMore.rawValue)
        })
        alert.addAction(okAction)
        alert.addAction(neverAgainAction)
        
        present(alert, animated: true, completion: nil)
    }

ViewController


override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        showDialogIfNeed()
    }

When I tried it, the OK button and Never Again button worked fine.

By the way, I made a Layout like this. Don't forget to watch viewDidLayoutSubviews ().

ViewController


let label = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.brown
        label.text = "Please see the Dialog"
        label.textAlignment = .center
        view.addSubview(label)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        label.frame = view.bounds
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        showDialogIfNeed()
    }

Recommended Posts

A function that displays only once while Alert is running and never displays it again
Find a value that is convenient to have a method and make it a ValueObject
[Rails] Volume that displays favorites and a list of favorites