Ich habe diese .html Daten in meinem Xcode-Projekt: Swift: Print Html Titel von WebView mit Swift und Javascript
Ich versuche, den Titel Wert mit diesen beiden Zeilen zu lesen:
document.title
document.getElementsByTagName(\"title\")[0].innerHTML;
aber es druckt nur:
Ergebnis ist: undefined
import UIKit
import JavaScriptCore
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
let localfilePath = NSBundle.mainBundle().URLForResource("home", withExtension: "html");
let myRequest = NSURLRequest(URL: localfilePath!);
myWebView.loadRequest(myRequest);
//let jsSource = "var testFunct = function(message) { return document.title;}"
let jsSource = "var testFunct = function(message) { return document.getElementsByTagName(\"title\")[0].innerHTML;}"
let context = JSContext()
context.evaluateScript(jsSource)
let testFunction = context.objectForKeyedSubscript("testFunct")
let result = testFunction.callWithArguments(["the message"])
print("result is: \(result)")
self.view.addSubview(myWebView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}