用户退出应用

时间:2015-05-13 20:22:22

标签: ios swift nsuserdefaults

我正处于游戏的最后阶段。我想将布尔值notYetRated保存到NSUserDefaults

bool用于查看用户是否对应用程序进行了评分(只需单击一个按钮即可对他进行评分)。

这是我设置它的方式:

var notYetRated = NSUserDefaults.standardUserDefaults()
func rateButtonPressed(sender:UIButton!)
{

    self.runAction(buttonSound, withKey: "buttonSound")
    var url  = NSURL(string: "itms://itunes.apple.com/app/id968231672")

    if UIApplication.sharedApplication().canOpenURL(url!) {
        UIApplication.sharedApplication().openURL(url!)
        if !notYetRated.boolForKey("notYetRated") {
            notYetRated.setBool(true, forKey: "notYetRated")
            scrollView.removeFromSuperview()
            SetUpScrollView()
        }

    }

}

出于某种原因,每次我杀了应用程序都没有用。价值仍然是假的。

任何人都有任何建议或看到我做错了什么?

2 个答案:

答案 0 :(得分:0)

只需添加synchronize即可将更改提交到NSUserDefaults的后端。

您的代码应该成为:

while (list != null)

documentations

似乎您的网址本身存在问题,可以使用适当的网址修复,例如:

var notYetRated = NSUserDefaults.standardUserDefaults()
func rateButtonPressed(sender:UIButton!)
{

    self.runAction(buttonSound, withKey: "buttonSound")
    var url  = NSURL(string: "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=app_id&mt=8")        
    if UIApplication.sharedApplication().canOpenURL(url!) {
        UIApplication.sharedApplication().openURL(url!)
        if !notYetRated.boolForKey("notYetRated") {
            notYetRated.setBool(true, forKey: "notYetRated")
            notYetRated.synchronize()
            scrollView.removeFromSuperview()
            SetUpScrollView()
        }

    }

}

List of supported schemes on iOS

答案 1 :(得分:0)

试试这样:

if let ratingURL = NSURL(string: "itms://itunes.apple.com/app/id968231672") {
    println("good url")
    if UIApplication.sharedApplication().canOpenURL(ratingURL) {
        if UIApplication.sharedApplication().openURL(ratingURL) {
            if !NSUserDefaults().boolForKey("notYetRated") {
                NSUserDefaults().setBool(true, forKey: "notYetRated")
                println(NSUserDefaults().boolForKey("notYetRated"))
            }
        }
    }
} else {
    println("bad url")
}