Login Use Alamofire And SwiftyJSON (Swift 4)


1. Create textField in story board or .xib file

import Alamofire
import SwiftyJSON
import UIKit

class LoginViewController: UIViewController, UITextFieldDelegate {
    
    
    @IBOutlet var emailTField: UITextField!
    @IBOutlet var passTField: UITextField!

2. Button action

@IBAction func bMasuk(_ sender: Any) {
        guard let email = emailTField.text, !(emailTField.text?.isEmpty)! else { return }
        guard let password = passTField.text, !(passTField.text?.isEmpty)! else { return }
        
        let vcbMasuk = HomeTabBarController()
        let urlLis = "https://yourbaseUrl/ios/v1/auth/login"
        
        let parameter: Parameters = [
            "email": email,
            "password": password
        ]
        
            
            Alamofire.request(urlLis, method: .post, parameters: parameter, encoding: URLEncoding.default, headers: nil)
                .validate(statusCode: 200..<300)
                .responseJSON { response in
                    
                    switch response.result {
                    case .success(let data):
                        print("isi: \(data)")
                        
                        self.navigationController?.pushViewController(vcbMasuk, animated: true)
                        
                    case .failure(let error):
                        alert.addAction(UIAlertAction(title: "Close", style: .default, handler: { (_: UIAlertAction) in
                            print("OK")
                        }))
                        
                        self.present(alert, animated: true, completion: nil)
                        print("need text")
                        print("Request failed with error: \(error)")
                 }
            }
    }


This my note as my problem and solution @luffyselah


No comments: