自动登录 WKwebview
Auto Login WKwebview
我有一个 swift 4 项目,我想要一个自动登录功能,这意味着当用户登录一次并重新启动应用程序时,他会自动登录我该怎么做这是我的代码:我使用 WKwebview 来执行此操作,如果有帮助,我使用 Swift 4。我还尝试添加一个进程池,如您在代码中所见,但它不起作用。
import UIKit
import WebKit
class viewHome: UIViewController, WKUIDelegate, WKNavigationDelegate {
weak var navigationDelegate: WKNavigationDelegate?
static let processPool = WKProcessPool()
@IBOutlet var webViewHome: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.processPool = viewHome.processPool
webViewHome = WKWebView(frame: .zero, configuration: webConfiguration)
webViewHome.uiDelegate = self
webViewHome.configuration.preferences.javaScriptEnabled = true
//webViewHome.configuration.preferences.javaEnabled = true
view = webViewHome
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.linopura.de/jtl.php")
let request = URLRequest(url: url!)
webViewHome.configuration.preferences.javaScriptEnabled = true
//webViewHome.configuration.preferences.javaEnabled = true
webViewHome.load(request)
}
@IBAction func GoBackHome(_ sender: Any) {
if webViewHome.canGoBack {
webViewHome.goBack()
}
}
@IBAction func GoForwardHome(_ sender: Any) {
if webViewHome.canGoForward {
webViewHome.goForward()
}
}
有什么想法吗?
提前致谢
如果您要求保持用户登录持久
在用户登录后登陆的 MainVC 中试试这个
func saveLoggedState() {
let def = UserDefaults.standard
def.set(true, forKey: "isLoggedin")
def.synchronize()
}
在 viewdidload 中调用 saveLoggedState()
在您的 Appdelegate 中
let def = UserDefaults.standard
let isLoggedin = def.bool(forKey: "isLoggedin")
if isLoggedin {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabbarController")
self.window?.rootViewController = initialViewController
}
我有一个 swift 4 项目,我想要一个自动登录功能,这意味着当用户登录一次并重新启动应用程序时,他会自动登录我该怎么做这是我的代码:我使用 WKwebview 来执行此操作,如果有帮助,我使用 Swift 4。我还尝试添加一个进程池,如您在代码中所见,但它不起作用。
import UIKit
import WebKit
class viewHome: UIViewController, WKUIDelegate, WKNavigationDelegate {
weak var navigationDelegate: WKNavigationDelegate?
static let processPool = WKProcessPool()
@IBOutlet var webViewHome: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.processPool = viewHome.processPool
webViewHome = WKWebView(frame: .zero, configuration: webConfiguration)
webViewHome.uiDelegate = self
webViewHome.configuration.preferences.javaScriptEnabled = true
//webViewHome.configuration.preferences.javaEnabled = true
view = webViewHome
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.linopura.de/jtl.php")
let request = URLRequest(url: url!)
webViewHome.configuration.preferences.javaScriptEnabled = true
//webViewHome.configuration.preferences.javaEnabled = true
webViewHome.load(request)
}
@IBAction func GoBackHome(_ sender: Any) {
if webViewHome.canGoBack {
webViewHome.goBack()
}
}
@IBAction func GoForwardHome(_ sender: Any) {
if webViewHome.canGoForward {
webViewHome.goForward()
}
}
有什么想法吗?
提前致谢
如果您要求保持用户登录持久
在用户登录后登陆的 MainVC 中试试这个
func saveLoggedState() {
let def = UserDefaults.standard
def.set(true, forKey: "isLoggedin")
def.synchronize()
}
在 viewdidload 中调用 saveLoggedState()
在您的 Appdelegate 中
let def = UserDefaults.standard
let isLoggedin = def.bool(forKey: "isLoggedin")
if isLoggedin {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabbarController")
self.window?.rootViewController = initialViewController
}