Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 如何设置会话&;cookie并将其储存起来,与Alamofire、Swift 3一起使用_Swift3_Session Cookies_Alamofire - Fatal编程技术网

Swift3 如何设置会话&;cookie并将其储存起来,与Alamofire、Swift 3一起使用

Swift3 如何设置会话&;cookie并将其储存起来,与Alamofire、Swift 3一起使用,swift3,session-cookies,alamofire,Swift3,Session Cookies,Alamofire,请有人给我一些指导,我在这方面是全新的。我正在制作一个具有登录选项的应用程序。一旦我登录,我想保存会话,这样我就不必反复登录,直到我注销并销毁会话。我正在为这个应用程序使用Alamofire和SwiftyJSON。这是我在按下“登录”按钮时的代码 @IBAction func loginBtnPressed(_ sender: AnyObject) { let userID = self.userName.text! as String let password = s

请有人给我一些指导,我在这方面是全新的。我正在制作一个具有登录选项的应用程序。一旦我登录,我想保存会话,这样我就不必反复登录,直到我注销并销毁会话。我正在为这个应用程序使用AlamofireSwiftyJSON。这是我在按下“登录”按钮时的代码

    @IBAction func loginBtnPressed(_ sender: AnyObject) {

    let userID = self.userName.text! as String
    let password = self.passwordText.text! as String

    if userID.isEmpty || password.isEmpty {
        showMessage(myMessage: "username & password required")
        return
    }

    let postsEndpoint: String = "http://xxx/api/login/"
    let newPost = ["username": userID, "password": password] as [String : Any]

    Alamofire.request(postsEndpoint, method: .post, parameters: newPost, encoding: URLEncoding.httpBody)
        .responseJSON { response in
            guard response.result.error == nil else {
                // got an error in getting the data, need to handle it
                print("error calling GET on /posts/1")
                print(response.result.error!)
                return
            }
            if let value: AnyObject = response.result.value as AnyObject? {
                // handle the results as JSON, without a bunch of nested if loops
                let post = JSON(value)
                print("The User is: " + post.description)

                let username = post["display_name"].string
                if  ( username != nil ) {
                   // to access a field:
                   // print("The title is: " + username!)

                    let sb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                    let controllerLogin : UIViewController = sb.instantiateViewController(withIdentifier: "logInSB")
                    self.present(controllerLogin, animated: true, completion: nil)

                    } else {
                      self.showMessage(myMessage: "wrong username & password")
                }
            }
    }
}

我知道什么是会话cookies,如果有好心人告诉我如何使用Alamofire&Swift实现它,这将是我的救命稻草。我正在网上搜索,但找不到合适的答案。先谢谢你

我自己解决了。在我的loginBtnPressed功能中,我设置了一些键,如下所示

UserDefaults.standard.set(post["user_id"].int, forKey: "user_id")
UserDefaults.standard.set(post["user_email"].stringValue, forKey: "user_email")
UserDefaults.standard.set(post["display_name"].stringValue, forKey: "display_name")
UserDefaults.standard.synchronize()
最后,在AppDelegate中,我检查并如下调用它

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    UIApplication.shared.statusBarStyle = .lightContent

    let getUserID = UserDefaults.standard.string(forKey: "user_id")

    if getUserID != nil {

        let sb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let controllerLogin : UIViewController = sb.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
        self.window?.rootViewController = controllerLogin
    }

    return true
}