I want to learn how to load a URL from foundation, resource with the network loading system.
How to load a URL ?
What do you want to do exactly ?
- load and open in a web page ?
@IBAction func testLoad(_ sender: Any) {
let website = "https://www.google.com/"
let vc = SFSafariViewController(url: URL(string: website)!)
if #available(iOS 13.0, *) {
vc.modalPresentationStyle = .overFullScreen
} else {
// Fallback on earlier versions
}
present(vc, animated: true)
}
- load data from a site ? For instance from a txt file:
var theText = ""
// 1. Build request for file
let url = URL(string:"https://someSite.com/TestAir.txt")!
let request = URLRequest(url: url)
// 2. Sends request with the completion, launch task
sendRequest(request: request) { (data) in
if let dataRead = data, let readText = String(data: dataRead, encoding: .utf8) {
theText = readText
}
}
- What else ?