WKWebView未在设备上运行

WKWebView not running on device

本文关键字:运行 WKWebView      更新时间:2024-04-28

我的主要问题是WKWebView。。。。当我运行我的应用程序时,它应该导航到后台的网站,并通过运行主捆绑包上的.js文件中的一些代码来登录用户。

 required init(coder aDecoder: NSCoder) {
 //  Load username and password from NSUserDefults
        self.staffID = defaults.objectForKey("UserName") as String
        self.pass = defaults.objectForKey("PassWord") as String
    // locate file from main bundle
        let populateLoginURL = NSBundle.mainBundle().pathForResource("login", ofType: "js")       
// edit username and password
      var PassEdited = pass + "'"
        println(pass)
        var PassJs = "document.getElementsByName('password')[0].value = '" + PassEdited
        var usernameEdited = "'" + staffID + "'"
        println(staffID)
        var userNameJS = "document.getElementsByName('username')[0].value = " + usernameEdited
    // submit fuction will validate username and password on webpage
        let submit = "submitButton()"
    //combine the java code in one line
       let javaCode = userNameJS + "; " + PassJs + "; " + submit
    // write java code to file in main bundle which is empty
      javaCode.writeToFile(populateLoginURL!, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
    //configure webView
       self.webView = WKWebView(frame: CGRectZero)
        let configuration = WKWebViewConfiguration()
    // load javacode to inject from file
        let populateLoginJS = String(contentsOfFile:populateLoginURL!,   encoding:NSUTF8StringEncoding, error: nil)
        let populateLoginScript = WKUserScript(source: populateLoginJS!,    injectionTime: .AtDocumentEnd, forMainFrameOnly: true)
// add configuration to webview
        configuration.userContentController.addUserScript(populateLoginScript)
                       configuration.preferences.javaScriptCanOpenWindowsAutomatically = false
        self.webView = WKWebView(frame: CGRectZero, configuration:      configuration)
    println(populateLoginJS)
    super.init(coder: aDecoder)
    }
 override func viewDidLoad() {
//set navigation delegate of web view and navigate to specified url
    webView.navigationDelegate = self
    let URL = NSURL(string: "https://ifly.etihad.ae")
    let request = NSURLRequest(URL:URL!)
    webView.loadRequest(request)
   } 

这一切都在IOS模拟器上没有出现故障。

话虽如此,我注意到最后一个println(populateLoginJs)通常打印.Js文件的内容,但当在我的设备上运行时,它会打印可选的

所以要么写JS文件不工作,要么加载JS文件不起作用这是我的直觉

伙计们,任何想法或信息都将受到高度赞赏

我设法解决了这个问题,发现写入文件从未在设备上编译。在联系了苹果的支持人员后,他们说这是一个他们知道的错误。

os我现在处理这个问题的方法是检查webView在哪个URL上,如果为true,那么webView.evaluateJavaScript