自动填充用户名和密码WKWebview Swift 3 Xcode

时间:2016-11-22 14:51:58

标签: xcode swift3 username wkwebview autofill

我正在尝试使用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.
}



}

2 个答案:

答案 0 :(得分:0)

这里有一件事非常重要,id(getElementById),请仔细检查您是否使用了正确的ID,这是在您的网络表单中使用的。

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

答案 1 :(得分:-1)

我知道它有点晚了,但这是我的代码,我的工作方式。这就是说服务器在构建它时期望字符串,但是我希望你有关于你的特定服务器如何构建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)

}

希望这有帮助。