我的主要问题是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文件不起作用那是我的预感
任何想法或信息都会受到高度赞赏答案 0 :(得分:0)
我设法解决了这个问题,我发现写入文件从未在设备上进行编译。在联系苹果支持后,他们说了一个他们知道的错误。
我正在处理这个问题的方法是检查webView所在的URL是否为true,然后是webView.evaluateJavaScript