ios - Cookie 不会让用户在 swift 3 中保持登录状态

标签 ios cookies uiwebview swift3

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

我打开应用程序,登录,关闭应用程序,当我再次打开它时,登录字段是空白的。 我希望至少用户名和密码是根据 cookie 填写的

这是来自 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")
    }

}

最佳答案

你已经完成了这里的一半工作,这很好但是

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

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

关于ios - Cookie 不会让用户在 swift 3 中保持登录状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41860796/

相关文章:

javascript - UIWebView 产生大约 :blank requests when iframe appended

html - Swift/Xcode - 是否可以控制网页上的 HTML 元素?

ios - Xcode 不显示build设置选项的菜单

javascript - 读取 Flask 未设置的 cookie

ssl - 使用 Curl、SSL 和 cookie 登录

php CURL - 多个独立 session - 需要帮助!

ios - 使用 Sencha touch 2 的 UIWebView 中的选项卡切换太慢

ios - 当整个屏幕设置为 RTL 时,我们如何将 tableview 单元格的语义更改为 LTR?

ios - AFNetworking 完成 block 等到完成

放大时使用 CGContextStrokePath() 绘制的 iOS 会滞后/崩溃