取得用户

时间:2019-04-02 16:49:47

标签: swift

我在获得用户许可时遇到问题,我的应用程序是关于用户询问和应用程序答案的。
我如何制作一个弹出窗口,当用户第一次打开该应用程序时,弹出窗口出现,并要求您保存问题。

2 个答案:

答案 0 :(得分:0)

1。显示警报视图:

let alert = UIAlertController()

alert.title = "This is your title of the alert"
alert.message = "This is your question"

alert.addAction(UIAlertAction(title: "Answer one", style: .default , handler:{ (UIAlertAction)in
// Let's save the user's choice for later
UserDefaults.standard.set("UserChoseOptionOne", forKey: "myReallyImportantQuestion")    
}))

alert.addAction(UIAlertAction(title: "Answer two", style: .default , handler:{ (UIAlertAction)in
// Let's save the user's choice for later 
UserDefaults.standard.set("UserChoseOptionTwo", forKey: "myReallyImportantQuestion")     

}))

alert.addAction(UIAlertAction(title: "Ask me later", style: .cancel, handler:{ (UIAlertAction)in

 // Let's not do anything here. 
 // The user can't decide at the moment and let's ask the next time your app is opened again 

}))

self.present(alert, animated: true, completion: nil)

2。在您的viewDidLoad

// Here we have to differentiate between three cases!

// Case 1: This is the very first time our dear user opened our cool app
// or
// Case 2: This is not the first time, we already asked our important question and we have an answer already!
// or
// Case 3: This is not the first time, the last time we asked our question, the user decided to choose later. Later is now!

if let option = UserDefaults.standard.string(forKey: "myReallyImportantQuestion") {

    // Here we handle Case 2. Maybe you don't want to do anything here
    // We already have the user's choice saved in option
    // We can do whatever we want with that

}else{

    // Here are the cases 1 and 3.
    // In here you can paste the code from "1. Display an alert view"
    // If you feel fancy today, you can also make a function out of "1. Display an alert view" and call it in here
}

答案 1 :(得分:-1)

创建userDefaults密钥 现在,您需要创建一个名为numberOfTimesLaunchedApp的新静态let,该静态let将用于读取和写入userDefaults,以便我们可以跟踪应用已打开多少次