I'm following a YouTube tutorial on how to make an IOS application with a Django backend and PostGres. I already have my authentication setup within SwiftUI, and it successfully connected with a "GET /api/account/ HTTP/1.1" 200 562 through my terminal. My API is up and running and successfully doing CRUD actions. Using postman, and their authentication tool, I could see my API working so that isn't where the issue lies.
By the way, I'm more familiar with Django and python than I am with Swift. Swift is new to me. I know that my ContentView file is what allows me to communicate with my API, but I'm not sure if I coded it correctly... Is this an issue that has something to do with my settings?
Here is what my ContentView looks like:
struct ContentView: View {
@State var accounts = [Account]()
var body: some View {
ForEach(accounts, id: \.self) {item in
HStack {
Image(systemName: "banknote").foregroundColor(.green)
Text(item.name)
Spacer()
Text("\(item.balance)")
}
}.onAppear(perform: loadAccount)
}
func loadAccount() {
guard let url = URL(string:
"http://127.0.0.1:8000/api/account/") else {
print("api is down")
return
}
let username = "N/A"
let password = "N/A"
let loginString = String(format: "%@:%@", username, password)
guard let loginData = loginString.data(using: String.Encoding.utf8) else {
return
}
let base64LoginString = loginData.base64EncodedString()
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
request.addValue("Basic N/A", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let response = try? JSONDecoder().decode([Account].self, from: data) {
DispatchQueue.main.async {
self.accounts = response
}
return
}
}
}.resume()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I can't seem to show any result on my simulator. It only displays a blank white screen.
Selecting any option will automatically load the page