I'm using Alamofire
for make a post request at the moment. I first built it in the ViewController
and worked. But then i'm trying to separate it by building it in another class
. I use a singleton
and still works when i call in the ViewController
. The only problem is after the status coming back a success i am trying to instantiate the another ViewController
when the user logs in. I don't want to do it in the networking class, i would like to do it inside my ViewController. How can i achieve this?
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()
}
}
Add completion block to your function :
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
}
}
}
And call it like this from ViewController:
NetworkManager.shared.makePostRequest(url: URL, params: Parameters, headers: HTTPHeaders, credential: "") { (success) in
if success {
} else {
}
}
You can pass the completion handler to your controller from Network manager class.
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
}
}
}
And now You can call this function as like below
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())
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.