I have been trying to make a pop up that ask's the user if they want to rate my app after a number of times they have opened it (say 5 times), with the options "yes"(send them to the app link) , "no" (discard) and "remind me later" (ask an again in an other 5 or 6 times ) any help would be much appreciated as I had done something similar in obj-c but having quite some trouble in swift, thanks !
Asked
Active
Viewed 941 times
2 Answers
0
You can use the NSUserDefaults to see how many times the app has been opened. See here : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/
You may increment your NSUserDefaults variable in the 'didFinishLaunchingWithOptions' method and then make your condition.
EDIT
Like this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let userDefaults = NSUserDefaults.standardUserDefaults();
let key = "nbTimesAppOpened"
userDefaults.setInteger(userDefaults.integerForKey(key) + 1, forKey: key)
userDefault.synchronize()
if userDefaults.integerForKey(key) >= 5 {
// Display your Alert
}
return true
}

Torreip
- 71
- 1
- 5
0
What about NSUserDefault + AlertAction ? Maybe something like this:
let userDefault = NSUserDefaults.standardUserDefaults()
let userDefaultOpenString = "AppOpenCounter"
let appStoreURLString = "itms://itunes.apple.com/en/your_url"
let countDetectorInt = 5
var openCounterInt = userDefault.integerForKey(userDefaultOpenString)
openCounterInt += 1
if openCounterInt == countDetectorInt {
// Create an alertController...
let alertController = UIAlertController(title: "Rate The App", message: "How about rate this app ?", preferredStyle: UIAlertControllerStyle.Alert)
// Add Yes Action
alertController.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { action in
userDefault.setInteger(0, forKey: userDefaultOpenString)
userDefault.synchronize()
if let url = NSURL(string: appStoreURLString) {
UIApplication.sharedApplication().openURL(url)
}
}))
//Add Not Now Action, so don't reset the userDefault Counter
alertController.addAction(UIAlertAction(title: "Not now", style: .Default, handler: nil))
//Add No Action
alertController.addAction(UIAlertAction(title: "No", style: .Default, handler: { action in
userDefault.setInteger(0, forKey: userDefaultOpenString)
userDefault.synchronize()
}))
}
userDefault.setInteger(openCounterInt, forKey: userDefaultOpenString)
userDefault.synchronize()
EDIT
WOOPS, forget to synchronize the UserDefault Dictionary ;)
Use this in your rootViewController.viewDidAppear() if you use a navigationController, or whenever you want

jlngdt
- 1,350
- 9
- 14
-
1
-
Thank, this looks like it's what i'm looking for but for some reason, nothing happens, with or without the userDefault.synchronize() but I guess it might be an issue with the simulator ? ( I did put that in the ViewDidLoad I assume this is correct ) – stephen D Sep 22 '15 at 15:48
-
@rmaddy when you process with userdefault, it's better to call it to avoid missunderstanding if you have many call on user default. I don't know his code so, I put it , that's all . Stephen, I forget the self.navigationcontroller.presentviewcontroller , to present it at the end my bad ;) – jlngdt Sep 22 '15 at 17:15
-
What misunderstanding? There is no misunderstanding. Read the docs for the `NSUserDefaults synchronize` method. It tells you that you really don't need it. It has nothing at all to do with how many calls you have on `NSUserDefaults`. – rmaddy Sep 22 '15 at 17:19
-
Discussion Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes – jlngdt Sep 22 '15 at 17:24
-
-
@jlngdt Do yo mean I have to add self.navigationcontroller.presentviewcontroller at the end of the code you posted? if so , I get an error message saying "ViewController does not have a member named 'navigationcontroller' – stephen D Sep 23 '15 at 12:38
-
`self.navigationController?.presentViewController(alertViewController, animated: true, completion: { _ in //do what you want when user click on button })` – jlngdt Sep 23 '15 at 12:50