My app’s login view accesses an external service to validate the user’s credentials and return a swift object.
The login view calls a login function here:
func login(completion: @escaping (Bool) -> Void) {
showProgressView = true
//checks with APIService
APIService.shared.login(credentials: credentials) { [unowned self](result:Result<Bool, Authentication.AuthenticationError>) in showProgressView = false
switch result {
case .success:
completion(true)
case .failure(let authError):
credentials = Credentials()
error = authError
completion(false)
}
}
}
The API service then does this:
URLSession.shared.dataTask(with: request) { data, HTTPURLResponse, Error in
typealias YourDecodedObject = [StudentClass]
if let data = data {
do {
let decodedObject = try JSONDecoder().decode(YourDecodedObject.self, from: data)
completion(.success(true))
} catch {
print("could not decode ❌")
print(error.localizedDescription)
completion(.failure(.invalidCredentials))
}
}
}.resume()
How would I send "YourDecodedObject" back to my login view?
2
Answers
SwiftUI answer
I recommend having all of the backend stuff in one class that conforms to
ObservableObject
.In this case, I named the class
ContentModel
and declared it in a file with the same name. Then you can access the data in whatever view by:Don’t forget to add
.environmentObject(ContentModel())
to the view you’re using the data in.PS: If you want to use Preview Provider, you’re gonna have to add there
.environmentObject(ContentModel())
too.If you want to share data from ContentModel across multiple targets open the file with ContentModel class, access the "Inspectors Menu" (on the right in Xcode) and go to the "File Inspector" tab. Locate "Target Membership"
and tick all the targets you want your data to be used in, then apply the same approach as above.
Hope this helps! It’s a complicated approach, but it’s worth it.
I believe, you wanted to pass
decodedObject
orAuthenticationError
objects back tofunc login
based on success or failure.If it is so then you need to modify your
completion
handler as below:here, you can pass success object i.e.
decodedObject
inData
andAuthenticationError
in error part.You don’t need to have
Bool
here to identifysuccess
orfailure
. You can just check either one for nil or data present and callcompletion
handler as follows:I made both callback response parameters as
nil
so it won’t be problem in any scenario.