我有一个带有用户身份验证的 swift WKWebView 应用程序,该应用程序允许用户通过收集设备的供应商 ID 来注册他们的设备。当用户运行移动应用程序时,它将检查移动设备是否已注册,然后验证与注册设备关联的登录详细信息。到目前为止一切都很好。
我的问题是当 PHP 会话过期时,用户将被重定向到登录页面。如果用户关闭应用程序并重新打开它,它将记录用户,而无需重新输入登录详细信息。由于公司政策,无法更改 PHP 会话超时。
我想知道我是否可以检测到登录页面 URL 更改并使用类似于应用程序启动过程的登录页面 URL 发送设备供应商 ID。因此,当 PHP 会话到期时,用户将自动重新登录,而无需重新输入登录凭据。
这是我正在使用的当前代码,这是我的第一个 IOS 应用程序,在移动编程方面我的经验非常有限,而且我最近几天一直在寻找没有任何运气。示例代码受到高度赞赏。
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// get device vendor id
let mystring = UIDevice.current.identifierForVendor!.uuidString
let webUrl = "https://www.mydomain/login/" + mystring
let myURL = URL(string: webUrl)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler(true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(false)
}))
present(alertController, animated: true, completion: nil)
}
}