RahinRahi

Swift example of login

Dec 18th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.42 KB | None | 0 0
  1. import Foundation
  2. import UIKit
  3.  
  4. import SwiftyJSON
  5. import SCLAlertView
  6. import SwiftValidator
  7. import Alamofire
  8. import Ipify
  9.  
  10. class LoginController: UIViewController,ValidationDelegate {
  11.    
  12.     @IBOutlet weak var login_bgcard: UIView!
  13.     @IBOutlet weak var loginBtn: UIButton!
  14.     @IBOutlet weak var loginbtnVw: UIView!
  15. //
  16.     @IBOutlet weak var uiTableview: UITableView!
  17.     @IBOutlet weak var parentView: UIView!
  18.    
  19.     @IBOutlet weak var chldView: UIView!
  20.     @IBOutlet var mainView: UIView!
  21.    
  22.  
  23.     @IBOutlet weak var domainLabel: UILabel!
  24.     @IBOutlet weak var emailTextOutlet: UITextField!
  25.     @IBOutlet weak var passwordTextOutlet: UITextField!
  26.    
  27.     @IBOutlet weak var changePassRequestBgCard: UIView!
  28.     @IBOutlet weak var requstpassBtn: UIView!
  29.    
  30.     @IBOutlet weak var changePassBackToLoginBtn: UIButton!
  31.    
  32.    
  33.     @IBOutlet weak var setPassBgCard: UIView!
  34.     @IBOutlet weak var setPasswordBtn: UIView!
  35.  
  36.      override func viewDidLoad() {
  37.      
  38.        // get ip address on view load
  39.        if(self.ipaddr.isEmpty){
  40.                 self.ipaddr = "127.0.0.1"
  41.                 CommonExtentions.getPublicIp(success: {
  42.                     (ip) -> Void in
  43.                     self.ipaddr = ip
  44.                     self.getToken()
  45.                     print("ip is \(ip)  ");
  46.                    
  47.                   }) {
  48.                     (error) -> Void in
  49.                      SCLAlertView().showError("Error!", subTitle: "Unable to get ip address",closeButtonTitle:"OK")
  50.                    }
  51.        }
  52.        self.initValidator()
  53.      }
  54.  
  55.      func getToken(){
  56.        
  57.         let parameters: Parameters = [
  58.             "ip_address": self.ipaddr,
  59.             ]
  60.         print("params \(parameters)")
  61.         DispatchQueue.main.async {
  62.            
  63.             WinparseAPI.requestPOSTURwitoutLoader("Authentication/getToken", params:parameters as [String : AnyObject], headers: [:] , success: {
  64.                 (JSONResponse) -> Void in
  65.                 print(JSONResponse)
  66.                 //                let json = JSON(JSONResponse)
  67.                 self.token = JSONResponse["token"].stringValue
  68.                 print("token is \(self.token) and json is ");
  69.                
  70.             }) {
  71.                 (error) -> Void in
  72.                 SCLAlertView().showError("Error!", subTitle: "Invalid domain url please change",closeButtonTitle:"OK")
  73.                
  74.             }
  75.            
  76.         }
  77.        
  78.        
  79.      }
  80.  
  81.      /* Initialize login form validation
  82.      /* Added by rahi
  83.      */
  84.      func initValidator(){
  85.        
  86.         validator.registerField(self.emailTextOutlet, rules: [RequiredRule(message: "Email address is required")])
  87.         validator.registerField(self.passwordTextOutlet, rules: [RequiredRule(message: "Password is required")])
  88.        
  89.     }
  90.  
  91.     func validationSuccessful() {
  92.         // submit the form
  93.         //Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
  94.         let parameters: Parameters = [
  95.             "ip_address": self.ipaddr,
  96.             "token"     : self.token,
  97.             "username"  : self.emailTextOutlet.text ?? "",
  98.             "password"  : self.passwordTextOutlet.text ?? "",
  99.             "device"    : "iphone",
  100.             "device_id"    : UIDevice.current.identifierForVendor!.uuidString,
  101.             ]
  102.         // print("paramas are \(parameters)")
  103.         WinparseAPI.requestPOSTURL("Authentication/postLogin", params:parameters as [String : AnyObject],
  104.            headers: [:] , success: {
  105.             (JSONResponse) -> Void in
  106.             print(JSONResponse)
  107.             if(JSONResponse["status"].intValue==200){
  108.                
  109.              self.commonExt.setSessionAsString("user_token",JSONResponse["user_token"].stringValue)
  110.              self.commonExt.setSessionAsString("first_name",JSONResponse["first_name"].stringValue)
  111.              self.commonExt.setSessionAsString("last_name",JSONResponse["last_name"].stringValue)
  112.              self.commonExt.setSessionAsString("gender",JSONResponse["gender"].stringValue)
  113.              self.commonExt.setSessionAsString("phone_no",JSONResponse["phone_no"].stringValue)
  114.              self.commonExt.setSessionAsString("city",JSONResponse["city"].stringValue)
  115.              self.commonExt.setSessionAsString("dob",JSONResponse["dob"].stringValue)
  116.              self.commonExt.setSessionAsString("image",JSONResponse["image"].stringValue)
  117.              self.commonExt.setSessionAsString("salt",JSONResponse["salt"].stringValue)
  118.            
  119.              self.showWInparstoastSuccess(text: "You have logged in successfully");
  120.              let storyboard = UIStoryboard(name: "home", bundle: nil)
  121.              let vc = storyboard.instantiateViewController(withIdentifier: "homepage") as! HomeTabController
  122.              self.present(vc, animated: true, completion: nil)
  123.              
  124.                
  125.             }else{
  126.               SCLAlertView().showError("Error!", subTitle: JSONResponse["message"].stringValue,closeButtonTitle:"OK")
  127.             }
  128.            
  129.         }) {
  130.             (error) -> Void in
  131.             SCLAlertView().showError("Error!", subTitle: "Error connecting to server",closeButtonTitle:"OK")
  132.            
  133.         }
  134.        
  135.     }
  136.    
  137.     func validationFailed(_ errors:[(Validatable ,ValidationError)]) {
  138.        
  139.         self.showWInparstoast(text: self.commonExt.getFirstErrorMessageOfSwiftValidator(errors)!)
  140.     }
  141.  
  142.      
  143.  
  144. }
Add Comment
Please, Sign In to add comment