我正在使用Xcode 8.2.1开发一个简单的Webview应用程序。
是的,我知道。有可用的较新版本,但是我分配的Mac不会更新为High Sierra。我被卡皮坦卡住了。因此,我遵循了一些简单的教程,并开始开发将加载我的网站的简单应用程序。 该应用程序本身可以运行,但是有一个 big 轻微问题:当用户关闭该应用程序时,Cookie将自行销毁,并且用户将不得不再次登录。 如果他们不小心关闭了应用程序或类似内容,我不想让他们再次登录。 我怎样才能做到这一点?我读了here我可以使用的
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
HTTPCookieStorage.shared.cookieAcceptPolicy = HTTPCookie.AcceptPolicy.always
return true
}
,但是很遗憾,它将无法正常运行,并且在关闭应用程序后,登录信息将清除。
这是ViewController.swift
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var myWebView: UIWebView!
@IBAction func backButton(_ sender: Any)
{
if myWebView.canGoBack{
myWebView.goBack()
}
}
@IBAction func refreshButton(_ sender: Any)
{
myWebView.reload()
}
@IBAction func forwardButton(_ sender: Any)
{
if myWebView.canGoForward{
myWebView.goForward()
}
}
func webViewDidStartLoad(_ webView: UIWebView) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func webViewDidFinishLoad(_ webView: UIWebView) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
override func viewDidLoad() {
super.viewDidLoad()
myWebView.loadRequest(URLRequest(url: URL(string: "myURL")!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
这是AppDelegate.swift
import UIKit
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
HTTPCookieStorage.shared.cookieAcceptPolicy = HTTPCookie.AcceptPolicy.always
return true
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
func saveCookies() {
guard let cookies = HTTPCookieStorage.shared.cookies else {
return
}
let array = cookies.flatMap { (cookie) -> [HTTPCookiePropertyKey: Any]? in
cookie.properties
}
UserDefaults.standard.set(array, forKey: "cookies")
UserDefaults.standard.synchronize()
}
func loadCookies() {
guard let cookies = UserDefaults.standard.value(forKey: "cookies") as? [[HTTPCookiePropertyKey: Any]] else {
return
}
cookies.forEach { (cookie) in
guard let cookie = HTTPCookie.init(properties: cookie) else {
return
}
HTTPCookieStorage.shared.setCookie(cookie)
}
}
}
此应用不会在应用商店中发布,仅适用于我公司中的三个用户。这就是为什么我决定做一件简单的事情而不是大规模的事情的原因。
答案 0 :(得分:0)
我只需要重新设置cookie。
NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:@"MySavedCookies"];
if([cookiesdata length]) {
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
NSHTTPCookie *cookie;
for (cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}