Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import UIKit
- import SwiftyJSON
- import SCLAlertView
- import SwiftValidator
- import Alamofire
- import Ipify
- class LoginController: UIViewController,ValidationDelegate {
- @IBOutlet weak var login_bgcard: UIView!
- @IBOutlet weak var loginBtn: UIButton!
- @IBOutlet weak var loginbtnVw: UIView!
- //
- @IBOutlet weak var uiTableview: UITableView!
- @IBOutlet weak var parentView: UIView!
- @IBOutlet weak var chldView: UIView!
- @IBOutlet var mainView: UIView!
- @IBOutlet weak var domainLabel: UILabel!
- @IBOutlet weak var emailTextOutlet: UITextField!
- @IBOutlet weak var passwordTextOutlet: UITextField!
- @IBOutlet weak var changePassRequestBgCard: UIView!
- @IBOutlet weak var requstpassBtn: UIView!
- @IBOutlet weak var changePassBackToLoginBtn: UIButton!
- @IBOutlet weak var setPassBgCard: UIView!
- @IBOutlet weak var setPasswordBtn: UIView!
- override func viewDidLoad() {
- // get ip address on view load
- if(self.ipaddr.isEmpty){
- self.ipaddr = "127.0.0.1"
- CommonExtentions.getPublicIp(success: {
- (ip) -> Void in
- self.ipaddr = ip
- self.getToken()
- print("ip is \(ip) ");
- }) {
- (error) -> Void in
- SCLAlertView().showError("Error!", subTitle: "Unable to get ip address",closeButtonTitle:"OK")
- }
- }
- self.initValidator()
- }
- func getToken(){
- let parameters: Parameters = [
- "ip_address": self.ipaddr,
- ]
- print("params \(parameters)")
- DispatchQueue.main.async {
- WinparseAPI.requestPOSTURwitoutLoader("Authentication/getToken", params:parameters as [String : AnyObject], headers: [:] , success: {
- (JSONResponse) -> Void in
- print(JSONResponse)
- // let json = JSON(JSONResponse)
- self.token = JSONResponse["token"].stringValue
- print("token is \(self.token) and json is ");
- }) {
- (error) -> Void in
- SCLAlertView().showError("Error!", subTitle: "Invalid domain url please change",closeButtonTitle:"OK")
- }
- }
- }
- /* Initialize login form validation
- /* Added by rahi
- */
- func initValidator(){
- validator.registerField(self.emailTextOutlet, rules: [RequiredRule(message: "Email address is required")])
- validator.registerField(self.passwordTextOutlet, rules: [RequiredRule(message: "Password is required")])
- }
- func validationSuccessful() {
- // submit the form
- //Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
- let parameters: Parameters = [
- "ip_address": self.ipaddr,
- "token" : self.token,
- "username" : self.emailTextOutlet.text ?? "",
- "password" : self.passwordTextOutlet.text ?? "",
- "device" : "iphone",
- "device_id" : UIDevice.current.identifierForVendor!.uuidString,
- ]
- // print("paramas are \(parameters)")
- WinparseAPI.requestPOSTURL("Authentication/postLogin", params:parameters as [String : AnyObject],
- headers: [:] , success: {
- (JSONResponse) -> Void in
- print(JSONResponse)
- if(JSONResponse["status"].intValue==200){
- self.commonExt.setSessionAsString("user_token",JSONResponse["user_token"].stringValue)
- self.commonExt.setSessionAsString("first_name",JSONResponse["first_name"].stringValue)
- self.commonExt.setSessionAsString("last_name",JSONResponse["last_name"].stringValue)
- self.commonExt.setSessionAsString("gender",JSONResponse["gender"].stringValue)
- self.commonExt.setSessionAsString("phone_no",JSONResponse["phone_no"].stringValue)
- self.commonExt.setSessionAsString("city",JSONResponse["city"].stringValue)
- self.commonExt.setSessionAsString("dob",JSONResponse["dob"].stringValue)
- self.commonExt.setSessionAsString("image",JSONResponse["image"].stringValue)
- self.commonExt.setSessionAsString("salt",JSONResponse["salt"].stringValue)
- self.showWInparstoastSuccess(text: "You have logged in successfully");
- let storyboard = UIStoryboard(name: "home", bundle: nil)
- let vc = storyboard.instantiateViewController(withIdentifier: "homepage") as! HomeTabController
- self.present(vc, animated: true, completion: nil)
- }else{
- SCLAlertView().showError("Error!", subTitle: JSONResponse["message"].stringValue,closeButtonTitle:"OK")
- }
- }) {
- (error) -> Void in
- SCLAlertView().showError("Error!", subTitle: "Error connecting to server",closeButtonTitle:"OK")
- }
- }
- func validationFailed(_ errors:[(Validatable ,ValidationError)]) {
- self.showWInparstoast(text: self.commonExt.getFirstErrorMessageOfSwiftValidator(errors)!)
- }
- }
Add Comment
Please, Sign In to add comment