如何检查是否存在密钥,如果不存在则写一个密钥?

时间:2014-12-23 08:01:28

标签: ios swift

以下代码的目的是记录用户,并生成随机字符串作为设备的标识符,但是我无法找出检查用户是否已获得ID并且已保存的方法作为关键。

@IBAction func loginTapped(sender: UIButton) {

    //generates string as ID for licencing
    func randomStringWithLength (len : Int) -> NSString {

        let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

        var randomString : NSMutableString = NSMutableString(capacity: len)

        for (var i=0; i < len; i++){
            var length = UInt32 (letters.length)
            var rand = arc4random_uniform(length)
            randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
        }

        return randomString
    }


    var userDefaults = NSUserDefaults.standardUserDefaults()
    var secretKey = ""
    var team:NSString = teamID.text
    var scout:NSString = scoutID.text
    var password:NSString = pinNumber.text

如果登录正确,则执行以下操作

                    if(success == 1 && secretKey != ""){

                    println("Login SUCCESS w/ key")
                    userDefaults.setValue(team, forKey: "teamid")
                    userDefaults.setValue(scout, forKey: "scout")
                    userDefaults.setValue(password, forKey: "password")
                    userDefaults.setValue(secretKey, forKey: "secretkey")
                    userDefaults.synchronize()
                    self.performSegueWithIdentifier("secondViewController", sender: self)


                } else if success == 1 && secretKey  == "" {
                    var secretKey = randomStringWithLength(15);
                    userDefaults.setValue(team, forKey: "teamid")
                    userDefaults.setValue(scout, forKey: "scout")
                    userDefaults.setValue(password, forKey: "password")
                    userDefaults.setValue(secretKey, forKey: "secretkey")
                    userDefaults.synchronize()
                    println("Login SUCCESS w/o key")
                    self.performSegueWithIdentifier("secondViewController", sender: self)



                    }

0 个答案:

没有答案