Cookies不会让用户登录swift3 [英] Cookies don´t keep user logged in in swift3

查看:88
本文介绍了Cookies不会让用户登录swift3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有UIWebView的简单应用程序,但是我不能使用cookie来存储登录信息或保持用户登录状态。

I have this simple application with a UIWebView, but I can not use the cookies to store the loggin or keep the user logged.

我打开应用程序,我登录时,我关闭了该应用程序,当我再次打开它时,登录字段为空白。
我至少希望用户和密码是基于cookie填充的

I open the application, I log in, I close the application, and when I open it again the login fields are blank. I would like at least that the user and password were filled based on cookies

这是viewController.swift中的代码

this is the code from viewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet var myWebView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = URL(string: "https://dashboardgrouping.correadasilva.com")

        myWebView.loadRequest(URLRequest(url:url!))


    }

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

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)->Bool {
        loadHTTPCookies()

        return true
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        saveCookies()
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        loadHTTPCookies()
    }

    func applicationWillTerminate(_ application: UIApplication) {
        saveCookies()
    }

    func loadHTTPCookies() {

        if let cookieDict = UserDefaults.standard.value(forKey: "cookieArray") as?NSMutableArray {

            for c in cookieDict {

                let cookies = UserDefaults.standard.value(forKey: c as!String) as!NSDictionary
                let cookie = HTTPCookie(properties: cookies as![HTTPCookiePropertyKey: Any])

                HTTPCookieStorage.shared.setCookie(cookie!)
            }
        }
    }

    func saveCookies() {

        let cookieArray = NSMutableArray()
        if let savedC = HTTPCookieStorage.shared.cookies {
            for c: HTTPCookie in savedC {

                let cookieProps = NSMutableDictionary()
                cookieArray.add(c.name)
                cookieProps.setValue(c.name, forKey: HTTPCookiePropertyKey.name.rawValue)
                cookieProps.setValue(c.value, forKey: HTTPCookiePropertyKey.value.rawValue)
                cookieProps.setValue(c.domain, forKey: HTTPCookiePropertyKey.domain.rawValue)
                cookieProps.setValue(c.path, forKey: HTTPCookiePropertyKey.path.rawValue)
                cookieProps.setValue(c.version, forKey: HTTPCookiePropertyKey.version.rawValue)
                 cookieProps.setValue(NSDate().addingTimeInterval(2629743), forKey: HTTPCookiePropertyKey.expires.rawValue)

                UserDefaults.standard.setValue(cookieProps, forKey: c.name)
                UserDefaults.standard.synchronize()
            }
        }

        UserDefaults.standard.setValue(cookieArray, forKey: "cookieArray")
    }

}


推荐答案

您在这里完成了一半的工作,很棒,但是

You have done half of the work here which is great but

您需要在 AppDelegate.swift中调用 loadHTTPCookies() saveCookies() 文件而不在您的viewController中

You need to call loadHTTPCookies() and saveCookies() in AppDelegate.swift file and not in your viewController

我为您构建了一个示例项目来查看:
https://github.com/omiz/Cookies

I built you a sample project to take a look at: https://github.com/omiz/Cookies

这篇关于Cookies不会让用户登录swift3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆