In swift 2.1, Xcode7.1.1
My code loads a local index.html into a WKwebView. How can I have a reference to the textFields so that I can set some of their properties? like myWebViewTextField.userInteractionEnabled = false
or myWebViewTextField.enabled = false
The docs says:
You get a window WebScriptObject object by sending windowScriptObject to your WebView object.
I am not sure how to go from here. Thank
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet var containerView: UIView! = nil //allows the class to refrence WKWebView
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView!
}
override func viewDidLoad() {
super.viewDidLoad()
//path explained http://www.cs.tut.fi/~jkorpela/fileurl.html
let baseUrl = NSURL(string: "file:///<path>/")
let path = NSBundle.mainBundle().pathForResource("index", ofType: "html")
let HTMLString: NSString?
do {
HTMLString = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
webView!.loadHTMLString(HTMLString as! String, baseURL: baseUrl )
} catch {
HTMLString = nil
}
}
You're reading the wrong class' docs. WebView and WKWebView are not the same. The latter is faster and more secure but not as manipulable.
For WKWebView, you'd want to use wkwebview.evaluateJavaScript to send a whole string of code that does the manipulations to the DOM. The completionHandler can asynchronously receive simple results like strings and numbers and fire off more JS evaluations based on those, but you can't receive DOM element references and directly tweak their properties from Swift.