Guest User

Untitled

a guest
Dec 8th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. //Calling Function code to generate OTP
  2. @IBAction func loginButtonTapped(_ sender: AnyObject) {
  3. print("mobile",self.textFieldPhoneNum.text!)
  4.  
  5. guard (textFieldName.text?.characters.count)! > 0 && (textFieldPhoneNum.text?.characters.count)! == 10 else {
  6. _ = SCLAlertView().showError(errText, subTitle: INPUT_PHONE_ERR,closeButtonTitle: closeButtonTitle, colorStyle : SCLAlertViewStyle.easyPolicy.defaultColorInt)
  7. return
  8. }
  9. self.nsConstantMethods.putString(key: NS_NAME_KEY,value: textFieldName.text!)
  10. self.nsConstantMethods.putString(key: NS_PHONE_KEY,value: textFieldPhoneNum.text!)
  11. self.loginAPIManager.generateOTPService(name: textFieldName.text!,epAppRegID: "", mobile: self.textFieldPhoneNum.text!, success: { (data) -> Void in
  12. SVProgressHUD.dismiss()
  13. self.loginRespnse = data!
  14. self.nsConstantMethods.putInt(key: NS_CUSTOMERID_KEY,value: self.loginRespnse.customerId)
  15. DispatchQueue.main.async {
  16. self.openOTPPopup()
  17.  
  18. }
  19. }, failure: { (data) -> Void in
  20. SVProgressHUD.dismiss()
  21. })
  22. }
  23.  
  24. //Function being called to generate OTP
  25. import Foundation
  26. import Swift
  27. import Alamofire
  28. import AlamofireObjectMapper
  29. import ObjectMapper
  30. import SVProgressHUD
  31.  
  32. public class LoginAPIManager {
  33. var otp : Int! = 0
  34.  
  35. func generateOTPService(name : String,epAppRegID: String, mobile: String, success: @escaping (_ data: LoginResponse?) -> Void,failure: @escaping (_ _data: AnyObject?) -> Void) -> Void{
  36. print("mobile no ",mobile)
  37. let otpGenratorModel = LoginOTP()
  38. otpGenratorModel.login = OTPModel()
  39. otpGenratorModel.login.mobile = mobile
  40. otpGenratorModel.login.deviceID = DEVICE_ID
  41. otpGenratorModel.login.name = name
  42. otpGenratorModel.login.epAppRegID = epAppRegID
  43. let i = Mapper<LoginOTP>().toJSON(otpGenratorModel)
  44. print("i",i.debugDescription)
  45. let otpResponse = OTPResponse()
  46. SVProgressHUD.show(withStatus: "Loading")
  47. SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.dark)
  48. SVProgressHUD.setRingThickness(0.8)
  49. SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.black)
  50. //network request to generate OTP
  51. Alamofire.request(BASE_URL + OTP_GENRATOR_URL, method: .post, parameters: i, encoding: JSONEncoding.default).responseObject( mapToObject: otpResponse) { responseObj in
  52. //switch to valide response
  53. switch responseObj.result{
  54. case .success(_):
  55. print("otp respoose",otpResponse.errorCode)
  56.  
  57. //nested switch to validate response code
  58. switch otpResponse.errorCode{
  59. case SUCCESS_CODE?:
  60. DispatchQueue.global(qos: .background).async {
  61. success(otpResponse.loginResponse)
  62. }
  63. self.otp = Int(otpResponse.loginResponse.OTP)!
  64. print("self.otp ",self.otp,otpResponse.loginResponse.OTP)
  65. break
  66. case DEFAULT_ERR_CODE?:
  67. _ = SCLAlertView().showError(errText, subTitle: errSubtext,closeButtonTitle: closeButtonTitle, colorStyle : SCLAlertViewStyle.easyPolicy.defaultColorInt)
  68. failure("default error" as AnyObject?)
  69. break
  70. case TIMEOUT_ERR?:
  71. _ = SCLAlertView().showError(timeOutErr, subTitle: errSubtext,closeButtonTitle: closeButtonTitle, colorStyle : SCLAlertViewStyle.easyPolicy.defaultColorInt)
  72. failure("Timeout error" as AnyObject?)
  73. break
  74. default :
  75. _ = SCLAlertView().showError(errText, subTitle: errSubtext,closeButtonTitle: closeButtonTitle, colorStyle : SCLAlertViewStyle.easyPolicy.defaultColorInt)
  76. failure("default error" as AnyObject?)
  77.  
  78. break
  79. }
  80. break
  81. case .failure(let error):
  82. guard (error as NSError?) != nil else{
  83. return
  84. }
  85. print(error.localizedDescription)
  86. _ = SCLAlertView().showError("Oops Something Went wrong", subTitle: error.localizedDescription,closeButtonTitle:"OK", colorStyle : SCLAlertViewStyle.easyPolicy.defaultColorInt)
  87. break
  88. }
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment