WKWebView 不在设备上 运行
WKWebView not running on device
我的主要问题是 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 文件的内容,但是当 运行 在我的设备上时,它会打印 optional""
所以要么写入 JS 文件不起作用,要么加载到 JS 文件不起作用那是我的预感
伙计们,我们将不胜感激任何想法或信息
我设法解决了这个问题,我发现写入文件从未在设备上编译过。在联系苹果支持后,他们说这是一个他们知道的错误。
os 我现在处理这个问题的方式是检查 URL webView 是否打开,如果为真则 webView.evaluateJavaScript
我的主要问题是 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 文件的内容,但是当 运行 在我的设备上时,它会打印 optional""
所以要么写入 JS 文件不起作用,要么加载到 JS 文件不起作用那是我的预感
伙计们,我们将不胜感激任何想法或信息
我设法解决了这个问题,我发现写入文件从未在设备上编译过。在联系苹果支持后,他们说这是一个他们知道的错误。
os 我现在处理这个问题的方式是检查 URL webView 是否打开,如果为真则 webView.evaluateJavaScript