如何实现警报率的完整代码和查看应用商店

时间:2016-06-13 00:05:10

标签: ios swift

正如您在某些应用中看到的那样,会弹出一个警告,要求用户对iTunes中的应用进行评级,通常您可以选择的替代方案如下:当然会打开应用的费率页面。第二个选项是No Thanks,它会关闭警报,第三个选项通常可能会在稍后显示警报。

我想知道如何做到这一点。我希望在应用程序启动20次之后显示警报,如果可能的话。

请确保swift 2的代码 非常感谢您的回复。

当我使用这段代码时,即使我点击了" No Thanks"

func showRateMe() {
    var alert = UIAlertController(title: "Rate Us", message: "Thanks for using <APP NAME>", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Rate <APP NAME>", style: UIAlertActionStyle.Default, handler: { alertAction in
        UIApplication.sharedApplication().openURL(NSURL(string : "itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=<iTUNES CONNECT APP ID>")!)
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler: { alertAction in
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "neverRate")
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    alert.addAction(UIAlertAction(title: "Maybe Later", style: UIAlertActionStyle.Default, handler: { alertAction in
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    self.presentViewController(alert, animated: true, completion: nil)
}


override func viewDidLoad() {
    super.viewDidLoad()showRateMe()}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

知道,当我使用此代码时,警报根本不会出现

导入UIKit

类ViewController:UIViewController {

var iMinSessions = 3
var iTryAgainSessions = 6



func viewDidAppear(){
    showRateMe()
}
func showRateMe() {
    var alert = UIAlertController(title: "Rate Us", message: "Thanks for using <APP NAME>", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Rate <APP NAME>", style: UIAlertActionStyle.Default, handler: { alertAction in
        UIApplication.sharedApplication().openURL(NSURL(string : "itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=<iTUNES CONNECT APP ID>")!)
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler: { alertAction in
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "neverRate")
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    alert.addAction(UIAlertAction(title: "Maybe Later", style: UIAlertActionStyle.Default, handler: { alertAction in
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    self.presentViewController(alert, animated: true, completion: nil)
}

func rateMe() {
    var neverRate = NSUserDefaults.standardUserDefaults().boolForKey("neverRate")
    var numLaunches = NSUserDefaults.standardUserDefaults().integerForKey("numLaunches") + 1

    if (!neverRate && (numLaunches == iMinSessions || numLaunches >= (iMinSessions + iTryAgainSessions + 1)))
    {
        showRateMe()
        numLaunches = iMinSessions + 1
    }
    NSUserDefaults.standardUserDefaults().setInteger(numLaunches, forKey: "numLaunches")
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

1 个答案:

答案 0 :(得分:0)

这对我有用:

import UIKit

class ViewController: UIViewController {

var iMinSessions = 3
var iTryAgainSessions = 2


override func viewDidAppear(animated: Bool) {
    rateMe()
}

func showRateMe() {
    let alert = UIAlertController(title: "Rate Us", message: "Thanks for using <APP NAME>", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Rate <APP NAME>", style: UIAlertActionStyle.Default, handler: { alertAction in
        UIApplication.sharedApplication().openURL(NSURL(string : "itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=<iTUNES CONNECT APP ID>")!)
        NSUserDefaults.standardUserDefaults().setBool(true, forKey:"didRate")
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler: { alertAction in
        NSUserDefaults.standardUserDefaults().setBool(true, forKey:"didRate")
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    alert.addAction(UIAlertAction(title: "Maybe Later", style: UIAlertActionStyle.Default, handler: { alertAction in
        alert.dismissViewControllerAnimated(true, completion: nil)
    }))
    self.presentViewController(alert, animated: true, completion: nil)
}

func rateMe() {
    let didRate = NSUserDefaults.standardUserDefaults().boolForKey("didRate")

    let numLaunches = NSUserDefaults.standardUserDefaults().integerForKey("numLaunches") + 1
    NSUserDefaults.standardUserDefaults().setInteger(numLaunches, forKey: "numLaunches")

    if !didRate && (numLaunches == iMinSessions ||
                    numLaunches > iMinSessions && (numLaunches - iMinSessions) % iTryAgainSessions == 0 )
    {
        showRateMe()
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

我改变了什么: iMinSessions和iTryAgainSessions的数字分别为2和3,因为我没耐心!

我更改了viewDidAppear :收到一个bool,被覆盖(这可能是你的问题) 我打电话给rateMe,而不是swowRateMe

在showRateMe中,我更改了按钮&#34; Rate&#34;或按钮&#34;不,谢谢&#34;点击(我认为这是目标)。请注意,OpenURL对我不起作用。

在rateme中,我更改了didRate的布尔名称(因为它在开头是假的,在评级时变为true)

我改变了测试,所以

  • App未评级且
    • numLaunches =初始最小值或
    • numLauches&gt;初始最小值,以及自初始最小值以来的lauches数是iTryAgainSessions的倍数:我的数字:当numLauches = 3 + 2或numLauches = 3 + 2 + 2,或numLaunches = 3 + 2 + 2 + 2时,......

我希望我能理解。