I want to take away WebService call from UIViewController 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&lt;Api&gt;()

  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&lt;Api&gt;()

  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?
I would recommend looking into MVVM architecture. You can see a simple example in this repo.
One problem with it is that its author declares and assigned view models directly in a ViewController which can be a bit problematic when writing tests. A better approach would be to inject view model so it can be easily replaced with mock. To improve that approach you can take a look at coordinators patterns, a bit more advanced example can be found here
In most cases the WebService should go in the ViewModel in the MVVM pattern. Services can be injected to ViewModels, which are responsible for managing data provided by these services. Then when they perform all logical transformations (filtering, ordering, multiplying etc.) they provide this information to a view (ViewController in our case) which just consumes it. It allows us to easily write mocks and tests. There are of course more approaches like state machine, but ViewModel as content provider and meeting point for services is most common and most universal one in my opinion.