I am using Moya to make network calls in Swift.
In my ViewController
I am calling the WebService and creating a model from response.
import UIKit
import Moya
class LoginViewController: UIViewController {
let provider = MoyaProvider<Api>()
override func viewDidLoad() {
super.viewDidLoad()
generateAccessToken()
}
private func generateAccessToken() {
provider.request(.generateAccessToken(
mobile: phoneTextField.text!,
countryCode: "91",
name: "Saurabh",
otp: "9612")
) { result in
switch result {
case .success(let response):
let data = response.data
let user = try? JSONDecoder().decode(User.self, from: data)
if let authToken = user?.authToken {
AuthManager.setToken(token: authToken)
}
case .failure(let error):
print(error.response?.statusCode as Any)
}
}
}
I want to move the WebService call to other file, say NetworkManager.swift
import Foundation
import Moya
struct NetworkManager {
public static let shared = NetworkManager()
private let provider = MoyaProvider<Api>()
func generateAccessToken(with mobile: String, countryCode: String, name: String, otp: String) {
// Get result and return to calling class
}
}
But it is strongly recommended not to use Singleton Class.
How should I call my WebService and return the response to the LoginViewController
?
Aucun commentaire:
Enregistrer un commentaire