I'm working on a simple webview app in Xcode 8.2.1.
Yes, I know. There's a newer version available, but the Mac I have assigned won't update to High Sierra. I'm stuck with El Capitan. So, I've followed some simple tutorials and I've come to develop a simple app that will load my website. The app itself works, but there's a slight big issue: when the users close the app, the cookies will destroy themselves and the user will have to login again. I don't want to make them login again if they accidentally close the app or something like that. How can I do this? I read here that I could use
@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
}
but, unfortunately it won't work and the login information will clear after the app is closed.
This is 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()
}
}
And this is 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)
}
}
}
This app won't be published on the appstore and it's intended just for three users in my company. This is why I decided to do a simple thing and not something big-scaled.
I just needed to re-set the cookies.