I\'ve found many ways to autofill a username and password in objective-c, but I can\'t figure out how to make it work with Swift. What would be the Swift equivalent of this
Here is a simple swift conversion of your provided code. SStringByEvaluatingJavaScriptFromString has been tested using different javascript and works correctly.
func webViewDidFinishLoad(webView: UIWebView!) {
let savedUsername = NSUserDefaults.standardUserDefaults().stringForKey("USERNAME")
let savedPassword = NSUserDefaults.standardUserDefaults().stringForKey("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)
}
}