在应用程序打开多次之后弹出,要求用户评价我的应用程序?

时间:2015-09-22 14:50:29

标签: ios swift

我一直想尝试弹出一个问用户,如果他们想要在他们打开它之后多次评价我的应用(比如说5次),选项"是& #34;(将它们发送到应用程序链接)," no" (丢弃)和"稍后提醒我" (在其他5或6次再问一次)任何帮助都会非常感激,因为我在obj-c中做了类似的事情,但在快速方面遇到了一些麻烦,谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用NSUserDefaults查看应用程序的打开次数。 见这里:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/

您可以在'didFinishLaunchingWithOptions'方法中增加NSUserDefaults变量,然后创建条件。

修改

像这样:

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
    }

答案 1 :(得分:0)

NSUserDefault + AlertAction怎么样?也许是这样的:

    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()

修改

WOOPS,忘记同步UserDefault词典;)

如果您使用navigationController,或者在需要时

,请在rootViewController.viewDidAppear()中使用它