我想在第一次打开应用程序时创建警报

时间:2017-06-08 13:38:24

标签: ios swift xcode

我想首次在设备上打开应用程序时创建警报,但我遇到了代码问题。这是我到目前为止,任何帮助将不胜感激

    // alert first time app is opened
    // making of alert

    let alert = UIAlertController(title: "Navigation", message: "Tap Right Hand Side of Screen For Next Quote, Left Hand Side To Go Back", preferredStyle: UIAlertControllerStyle.alert)

    //add ok button
    alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))

    // detect if first launch
    let launchedBefore = UserDefaults.standard.bool(forKey: "launcedBefore")
    if launchedBefore {
    }
    else {
        self.present(alert, animated: true, completion: nil)
        UserDefaults.standard.set(true, forKey: "launchedBefore")
    }

这是我试图放置代码的地方

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

2 个答案:

答案 0 :(得分:0)

launcedBefore键中有拼写错误 将其更改为:

let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")

答案 1 :(得分:0)

如果您在AppDelegate中尝试,则必须在window?.rootViewController?

中显示提醒
var alertController = UIAlertController(title: "Title", message: "Any message", preferredStyle: .ActionSheet)
var okAction = UIAlertAction(title: "Yes", style: 
UIAlertActionStyle.Default) {
                UIAlertAction in
                NSLog("OK Pressed")
            }
var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
                UIAlertAction in
                NSLog("Cancel Pressed")
            }
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)

检查let launchedBefore = UserDefaults.standard.bool(forKey: "launcedBefore")中的值(真或假)。显示提醒取决于bool值。