自动填充用户名和密码 WKWebview Swift 3 Xcode
Posted
技术标签:
【中文标题】自动填充用户名和密码 WKWebview Swift 3 Xcode【英文标题】:Autofill Username and Password WKWebview Swift 3 Xcode 【发布时间】:2016-11-22 14:51:57 【问题描述】:我正在尝试使用 WKWebView 类在 Swift 3 中制作应用程序以访问网站并自动填写用户名和密码。我已经尝试搜索了几天,但只能使用 Obj-C 和旧版本的 swift 找到答案。
我尝试使用这里提出的解决方案Autofill Username and Password UIWebView Swift
但是这在 Swift 3 中对我不起作用。这是我的示例代码:
import Foundation
import UIKit
import WebKit
class spot: UIViewController, UIWebViewDelegate
@IBOutlet weak var container: UIView!
var webView: WKWebView!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
webView = WKWebView()
container.addSubview(webView)
override func viewDidAppear(_ animated: Bool)
let frame = CGRect(x:0, y:0, width:container.bounds.width, height:container.bounds.height)
webView.frame = frame
// Insert URL below to change launched page
let urlStr = "https://login.salesforce.com/"
let url = NSURL(string: urlStr)!
let request = NSURLRequest(url: url as URL)
webView.load(request as URLRequest)
func webViewDidFinishLoad(_ webView: UIWebView!)
let savedUsername = UserDefaults.standard.string(forKey: "USERNAME")
let savedPassword = UserDefaults.standard.string(forKey: "PASSWORD")
if savedUsername == nil || savedPassword == nil return
if ( countElements(savedUsername!) != 0 && countElements(savedPassword!) != 0)
let loadUsernameJS = "var inputFields = document.querySelectorAll(\"input[name='username']\"); \\ for (var i = inputFields.length >>> 0; i--;) inputFields[i].value = \'\(savedUsername)\';"
let loadPasswordJS = "var inputFields = document.querySelectorAll(\"input[name='password']\"); \\ for (var i = inputFields.length >>> 0; i--;) inputFields[i].value = \'\(savedPassword)\';"
self.webView.stringByEvaluatingjavascriptFromString(loadUsernameJS)
self.webView.stringByEvaluatingJavaScriptFromString(loadPasswordJS)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【问题讨论】:
【参考方案1】:这里有一点非常重要,即 id (getElementById),请仔细检查您使用的 id 是否正确,该 id 用于您的 Web 表单。
func webViewDidFinishLoad(_ webView: UIWebView)
if (email == "" || password == "") return
// fill data
let fillForm = String(format: "document.getElementById('email').value = '\(email)';document.getElementById('pass').value = '\(password)';")
webView.stringByEvaluatingJavaScript(from: fillForm)
【讨论】:
【参考方案2】:我知道它有点晚了,但这是我如何使它工作的代码。这就是说服务器在我构造它时期待该字符串,但我希望您了解您的特定服务器如何期望构造 URLRequest。
@IBOutlet weak var loadingWebPage: UIActivityIndicatorView!
override func viewDidLoad()
super.viewDidLoad()
let savedUsername = UserDefaults.standard.string(forKey: "USERNAME")
let savedPassword = UserDefaults.standard.string(forKey: "PASSWORD")
let urlStr = "https://login.salesforce.com/"
myWebView = WKWebView()
myWebView.uiDelegate = self
myWebView.navigationDelegate = self
setUrlString = "\(urlStr)?username=\(savedUsername)&password=\(savedPassword)"
override func viewWillAppear(_ animated: Bool)
view.addSubview(myWebView)
myWebView.translatesAutoresizingMaskIntoConstraints = false
let width = NSLayoutConstraint(item: myWebView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1.0, constant: 0)
let height = NSLayoutConstraint(item: myWebView, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1.0, constant: -70)
let top = NSLayoutConstraint(item: myWebView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 70)
view.addConstraints([width,height,top])
self.myWebView.addSubview(loadingWebPage)
let myURL = URL(string: setUrlString)
let myRequest = URLRequest(url: myURL!)
myWebView.load(myRequest)
希望这会有所帮助。
【讨论】:
另外,您应该使用 KeyChain 而不是 UserDefaults 来安全地存储密码。以上是关于自动填充用户名和密码 WKWebview Swift 3 Xcode的主要内容,如果未能解决你的问题,请参考以下文章