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



我有一个简单的应用程序,带有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(),而不是在视图中控制器

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

我认为该函数从未被调用application didFinishLaunchingWithOptions launchOptions:因为它是UIApplicationDelegate的函数。大多数情况下,您只在默认情况下实现UIApplicationDelegateAppDelegate上使用这些函数

请尝试

loadHTTPCookies()移至viewDidLoadviewWillAppear

application didFinishLaunchingWithOptions launchOptions:回实现UIApplicationDelegate类(例如,例如 AppDelegate)

相关内容

  • 没有找到相关文章

最新更新