Ios Swift:使用evaluateJavascript

Ios Swift:使用evaluateJavascript,ios,swift,wkwebview,Ios,Swift,Wkwebview,我是Swift开发的新手,我正在开发一个混合应用程序,我已经连接了认证。当用户使用设备上的指纹传感器进行身份验证时,我希望触发JS或以其他方式与WKWebView交互。。。但由于某种原因,我似乎无法让它发挥作用。我可以做一些简单的事情,比如改变窗口HREF。。。但如果我做一些更复杂的事情,它要么什么也不做,要么失败 以下是我的viewController的代码: import UIKit import WebKit import LocalAuthentication

我是Swift开发的新手,我正在开发一个混合应用程序,我已经连接了认证。当用户使用设备上的指纹传感器进行身份验证时,我希望触发JS或以其他方式与WKWebView交互。。。但由于某种原因,我似乎无法让它发挥作用。我可以做一些简单的事情,比如改变窗口HREF。。。但如果我做一些更复杂的事情,它要么什么也不做,要么失败

以下是我的viewController的代码:

    import UIKit
    import WebKit
    import LocalAuthentication

    class ViewController: UIViewController, WKScriptMessageHandler, WKUIDelegate, WKNavigationDelegate {

        @IBOutlet var containerView : UIView! = nil

        // @IBOutlet weak var webView: UIWebView!
        var webView: WKWebView?
        var contentController = WKUserContentController();

        @IBOutlet var activityIndicatorView: UIActivityIndicatorView!

        override func viewDidLoad() {
            super.viewDidLoad()

            // append the userAgent and ensure it contains our browser detect regEx
            let userAgent = UIWebView().stringByEvaluatingJavaScriptFromString("navigator.userAgent")! + " iPad"
            NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : userAgent])

            // add JS content controller

            var config = WKWebViewConfiguration()
            config.userContentController = contentController

            // instantiate the web view
            let webView = WKWebView(frame: CGRectZero, configuration: config)
            webView.setTranslatesAutoresizingMaskIntoConstraints(false)
            webView.navigationDelegate = self
            view.addSubview(webView)

            // customize sizing
            view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: ["webView": webView]))

            view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: ["webView": webView]))

            // open the URL for the app
            let urlPath = "http://im_a_url"
            let url: NSURL = NSURL(string: urlPath)!
            let request = NSURLRequest(URL: url)
            webView.loadRequest(request)

        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        func webView(webView: WKWebView!, didStartProvisionalNavigation navigation: WKNavigation!) {
          UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        }

        func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) {
   UIApplication.sharedApplication().networkActivityIndicatorVisible = false

            // trigger authentication
            authenticateUser()

        }

        func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {

        }

        func logInWithStoredCredentials() {
            println("successful auth - log in");
            // TO DO - use core data to stor user credentials
            webView!.evaluateJavaScript("document.getElementById('anonymousFormSubmit').click();", nil)

        }

        func authenticateUser() {
            // Get the local authentication context.
            let context = LAContext()

            // Declare a NSError variable.
            var error: NSError?

            // Set the reason string that will appear on the authentication alert.
            var reasonString = "Authentication is needed to access aware360Suite."

            // Check if the device can evaluate the policy.
            if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
                [context .evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in

                    if success {
                        self.logInWithStoredCredentials()
                        // self.webView?.evaluateJavaScript("document.getElementById('auiAuthSubmitBtn').click();", nil)
                    }
                    else{
                        // If authentication failed then show a message to the console with a short description.
                        // In case that the error is a user fallback, then show the password alert view.
                        println(evalPolicyError?.localizedDescription)

                        switch evalPolicyError!.code {

                        case LAError.SystemCancel.rawValue:
                            println("Authentication was cancelled by the system")

                        case LAError.UserCancel.rawValue:
                            println("Authentication was cancelled by the user")

                        case LAError.UserFallback.rawValue:
                            println("User selected to enter custom password")
                            // self.showPasswordAlert()

                        default:
                            println("Authentication failed")
                            // self.showPasswordAlert()
                        }
                    }

                })]
            }
            else{
                // If the security policy cannot be evaluated then show a short message depending on the error.
                switch error!.code{

                case LAError.TouchIDNotEnrolled.rawValue:
                    println("TouchID is not enrolled")

                case LAError.PasscodeNotSet.rawValue:
                    println("A passcode has not been set")

                default:
                    // The LAError.TouchIDNotAvailable case.
                    println("TouchID not available")
                }


            }

        }

    }
问题在于成功的身份验证方法:

func logInWithStoredCredentials() {
        println("successful auth - log in");
        // TO DO - use core data to use stored user credentials
        webView!.evaluateJavaScript("document.getElementById('anonymousFormSubmit').click();", nil)

    }
我似乎无法控制这里的网络视图。如果我尝试在此处实际计算脚本,它将抛出以下错误:

2015-02-10 17:07:32.912 A360[2282:462860] -[UIWebView evaluateJavaScript:completionHandler:]: unrecognized selector sent to instance 0x1741897f0
2015-02-10 17:07:32.916 A360[2282:462860] <NSXPCConnection: 0x178103960> connection to service named com.apple.CoreAuthentication.daemon: Warning: Exception caught during decoding of received reply to message 'evaluatePolicy:options:reply:', dropping incoming message and calling failure block.

Exception: -[UIWebView evaluateJavaScript:completionHandler:]: unrecognized selector sent to instance 0x1741897f0
很明显,在我的logInWithStoredCredentials函数中,它丢失了webView的上下文

如何在我的logInWithStoredCredentials函数中正确处理webView


抱歉-我知道这是一个相当基本的问题,但我已经用了几个小时的时间来思考这个问题,这是一个非常紧迫的问题,我必须尽快解决。

您的UI中似乎有UIWebView而不是WKWebView:

Exception: -[UIWebView evaluateJavaScript:completionHandler:]: unrecognized selector sent to instance 0x1741897f0

您应该实例化WKWebView实例。另外请注意,不能在Interface Builder中创建WKWebView,您必须在运行时将其添加到视图层次结构中。

您有
var webView:WKWebView
?然后在
viewDidLoad
中将其重新定义为
let-webView=WKWebView(frame:CGRectZero,configuration:config)
删除webView前面的let,它应该可以工作。

看看这个新的,它可能会对您有所帮助;)
Exception: -[UIWebView evaluateJavaScript:completionHandler:]: unrecognized selector sent to instance 0x1741897f0