Created
August 1, 2021 19:04
-
-
Save sweeneyapps/8c88daed58fb7b6ccdad44fb5178fa50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// chat | |
// | |
// Created by Paul Sweeney Jr on 8/01/21. | |
// | |
import SwiftUI | |
class BlobModel: ObservableObject { | |
static let sharedInstance = BlobModel() | |
@Published var website: String = "Loading..." | |
} | |
var globalVariable = BlobModel() | |
struct ContentView: View { | |
var body: some View { | |
Text("\(globalVariable.website)") | |
.font(.title) | |
.padding().onAppear { | |
loadWebsite(url: | |
URL(string: "https://sweeneyapps.com")) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
// code for loadWebsite below | |
import Foundation | |
func loadWebsite(url: URL?) { | |
let task = URLSession.shared.dataTask(with: url!) { data, response, error in | |
if let error = error { | |
print(error) | |
return | |
} | |
guard let httpResponse = response as? HTTPURLResponse, | |
(200...299).contains(httpResponse.statusCode) else { | |
print("The httpResponse is not 2xx") | |
return | |
} | |
if let mimeType = httpResponse.mimeType, mimeType == "text/html", | |
let data = data, | |
let string = String(data: data, encoding: .utf8) { | |
globalVariable.website = string // output to UI | |
} | |
} | |
task.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment