代码之家  ›  专栏  ›  技术社区  ›  sk123

如何将网络代码与视图控制器分开

  •  1
  • sk123  · 技术社区  · 6 年前

    我在用 Alamofire 以便在此时提出邮寄请求。我第一次建造它是在 ViewController 然后工作。但是我想把它分开 class . 我用 singleton 当我打电话给 可视控制器 . 唯一的问题是,在状态恢复成功后,我试图实例化另一个状态 可视控制器 当用户登录时。我不想在网络课上做,我想在我的视图控制器里做。我怎样才能做到这一点?

    enum Status: Int {
        case Success = 200
        case Failure = 401
    }
    
    enum HTTPMethod: String {
        case Post = "POST"
        case Get = "GET" 
    }
    
    enum APIKey: String {
        case value = "somevalueeeeeeeee"
        case key = "somekeyyyyyyyyyyyyyy"
    }
    
    import Foundation
    import Alamofire
    import UIKit
    
    class NetworkManager {
        static let shared = NetworkManager()
    
        func makePostRequest(url: URL, params: Parameters, headers: HTTPHeaders, credential: URLCredential) {
            Alamofire.request(url, method: .post, parameters: params, headers: headers).authenticate(usingCredential: credential)
                .responseJSON { response in
                    print(response)
                    let status = response.response?.statusCode
                    switch status {
                    case Status.Success.rawValue:
                        print("I would like to instantiate my viewcontroller here")
                    case Status.Failure.rawValue:
                        print("Bad Request")
                    default:
                        break
                    }
            }
        }
    }
    
    import UIKit
    import Alamofire
    
    class LoginViewController: UIViewController {
        @IBOutlet weak var emailTextField: UITextField!
        @IBOutlet weak var passwordTextfield: UITextField!
    
        private func login() {
            guard let userName = emailTextField.text else { return }
            guard let passWord = passwordTextfield.text else { return }
    
            let params: Parameters = [
                "username": userName,
                "password": passWord
            ]
            guard let url = URL(string: URLS.url.rawValue) else { return }
            let headers = [APIKey.key.rawValue: APIKey.value.rawValue]
            let credential = URLCredential(user: userName, password: passWord, persistence: .forSession)
    
            NetworkManager.shared.makePostRequest(url: url, params: params, headers: headers, credential: credential)
        }
    
        @IBAction func onLoginTapped(_ sender: UIButton) {
                login()
            }
        }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Sharad Chauhan Muhammad Bilal Hussain    6 年前

    func makePostRequest(url: URL, params: Parameters, headers: HTTPHeaders, credential: URLCredential, completion:@escaping(Bool)->()) {
        Alamofire.request(url, method: .post, parameters: params, headers: headers).authenticate(usingCredential: credential)
            .responseJSON { response in
                print(response)
                let status = response.response?.statusCode
                switch status {
                case Status.Success.rawValue:
                    print("I would like to instantiate my viewcontroller here")
                    completion(true)
                    break
                case Status.Failure.rawValue:
                    print("Bad Request")
                    completion(false)
                    break
                default:
                    break
                }
        }
    }
    

    NetworkManager.shared.makePostRequest(url: URL, params: Parameters, headers: HTTPHeaders, credential: "") { (success) in
        if success {
    
        } else {
    
        }
    }
    
        2
  •  1
  •   Jaydeep Vora    6 年前

     func makePostRequest(url: URL, params: Parameters, headers: HTTPHeaders, credential: URLCredential, completion:@escaping (Result<Any>)->()) {
            Alamofire.request(url, method: .post, parameters: params, headers: headers).authenticate(usingCredential: credential)
                .responseJSON { response in
                    print(response)
                    completion(response.result)
                    let status = response.response?.statusCode
                    switch status {
                    case Status.Success.rawValue:
                        print("I would like to instantiate my viewcontroller here")
                         completion(response.result)
                        break
                    case Status.Failure.rawValue:
                        print("Bad Request")
                         completion(response.result)
                        break
                    default:
                        break
                    }
            }
        }
    

      NetworkManager.shared.makePostRequest(url: url, params: , headers: param, credential: credential, completion: { (response) in
             if result.isSuccess, let json = result.value {
                // Use json variable to handle json
            } else {
                self.showAlert(R.string.keys.somethingWentWrong())
            }
       })