JavaEar 专注于收集分享传播有价值的技术资料

Autofill Username and Password WKWebview Swift 3 Xcode

I am trying to make an app in Swift 3 using the WKWebView class to go to a website and autofill in the Username and password. I have tried search for days and only find answers using Obj-C and old versions of swift.

I tried using the solution proposed here Autofill Username and Password UIWebView Swift

However this did not work for me in Swift 3. Here is my example code:

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个回答

    最佳答案
  1. I know its a bit late but here is my code for how I have made it work. That said the server was expecting the string as I constructed it, but I am hoping you have the info on how your particular server is expecting the URLRequest to be constructed.

        @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)
    
    }
    

    Hope this helps.