Hello.
You can use the framework hpple for this. The framework is built in Objective-C, but you can use it in Swift. I made a simple example, which loads an HTML page with two Forms (distributed together with the app), parses the HTML and shows only the first form.
This is the HTML page:
<!DOCTYPE HTML>
<html>
<head>
<title>Test page with 2 forms</title>
</head>
<body>
<h1>Form 1</h1>
<form id="form1" action="your_action_here">
<input type="text" value="You name here">
<input type="submit">
</form>
<h1>Form 2</h1>
<form id="form2">
<input type="text" value="You name here">
<input type="submit">
</form>
</body>
</html>
I made a simple Viewcontroller, which finds and loads the first HTML form in the viewDidLoad method.
import UIKit
import hpple
class ViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
if let htmlFile = NSBundle.mainBundle().pathForResource("index", ofType: "html", inDirectory: "www") {
let htmlData = NSData(contentsOfFile: htmlFile)
let htmlDocument = TFHpple(HTMLData: htmlData)
if let htmlElements = htmlDocument.searchWithXPathQuery("//form[@id='form1']") as? [TFHppleElement] {
if let firstForm = htmlElements.first?.raw {
webView.loadHTMLString(firstForm, baseURL: nil)
}
}
} else {
print("File not found")
}
}
}
You can see the complete source code in this repository on Github. I used the Cocoapods to add the hpple dependency. If you want more information about Cocoapods, you can see this tutorial.
in Portuguese it is very difficult to find good materials, but in this link here you will find what you are looking for and in a very didactic way, I think: http://www.raywenderlich.com/14172/how-to-parse-html-on-ios I hope you help.
– Paulo Roberto
Also here: http://stackoverflow.com/questions/28261570/parsing-html-in-swift-other-than-using-regex
– Paulo Roberto
I need something that is purely in Swift, the material can be in Portuguese, English or Spanish
– Fábio Jânio