Reputation: 99
I want my app to display an alert view the first time someone uses my app, and the second time, it wont appear. I've searched around, but cant figure it out. I'm using swift.
Here's my alertview method:
var alertView = UIAlertView()
alertView.addButtonWithTitle("Ok")
alertView.title = "Information"
alertView.message = "Pinch the screen to scroll."
alertView.show()
Upvotes: 0
Views: 383
Reputation: 6077
let userDefaults = NSUserDefaults.standardUserdefaults()
if (userDefaults.valueForKey("alertViewShowedFirstTime") == nil) {
// alertview code
}
Then add this piece of code
userDefaults.setValue("showed", forKey: "alertViewShowedFirstTime")
userDefaults.synchronize()
I wrote it on the fly so it may not be error free but that's the main idea.
Just store in NSUserDefault
that you already showed it once.
Upvotes: 1
Reputation: 3120
You need to create some boolean flag and store it for example to NSUserdefauls. First time boolean going false. Then you showing your alert, then you should make boolean true and store it to ns userdefaults. Always check your bool var from nsuserdefaults if it's true or false.
Upvotes: 1