I'm trying to download some HTML from a website, parse it, and display it in a grid. The HTML isn't formatted very well, so I've already written an init() method that parses that HTML as a String.
My init() works fine in my unit tests.
What I'm having trouble with, is, getting the HTML as a string via Alamofire. All the examples I can find seem to imply that I need to do something close to this...
import SwiftUI
import Alamofire
func getHtml()-> String
{
let x = AF.request("https://httpbin.org/get")
.validate()
.response
if let y = x {
debugPrint(y)
return y.something()
}
return "<!-- the html could not be retrieved -->"
}
struct ContentView: View {
var body: some View {
Text(getHtml())
.padding()
}
}
My challenge is that AF.request().response seems to always be nil. Which I find strange because all the Alamofire examples I've seen online do not seem to even check for that. It's as if older of Alamofire returned an HTTPResponse, not an HTTPResponse?
Is there a simple way I can retrieve the (non-JSON) output of the website I'm looking at, and hand it to my constructor? With or without Alamofire?
Thanks!
This is with Alamofire 5.6.3 if it matters
Things I've tried:
- I've tried with / without the validate()
- On the theory response is asyrchonous, I tried adding a braindead while loop: while(response != null) { sleep(1) }