swift 提示框的使用

//短信验证码登陆 - 登陆

   func codeNumber(){

      let alertController = UIAlertController(title: "提示",message:"请输入验证码",preferredStyle: UIAlertControllerStyle.alert)

      alertController.addTextField { (textField: UITextField) in

         textField.placeholder = "请输入验证码"

         NotificationCenter.default.addObserver(self, selector: #selector(self.alertCodeTextFieldDidChange(_:)), name: NSNotification.Name.UITextFieldTextDidChange, object: textField)

 

      }

      let okAction = UIAlertAction(title: "登陆",style: UIAlertActionStyle.default){

         (action: UIAlertAction) -> Void in

         let userstr = self.phoneNumber

         if let code = alertController.textFields?.first?.text{

            HYUser.loginCode(userstr, code: code) { (error) in

               if error.success{

                  let url = URL(string: "https://www.XXXXXXXXX")

                  let body = "username=\(userstr.UTF8ToGB2312())&password=\(code)&remember=1"

                  var request = URLRequest(url: url!)

                  request.httpMethod = "POST"

                  request.httpBody = body.data(using: String.Encoding.utf8)

                  self.webView.loadRequest(request)

               }else{

                  self.loginHandler!(false)

               }

            }

         }

         NotificationCenter.default.removeObserver(self)

      }

      let cancelAction = UIAlertAction(title: "取消",style: UIAlertActionStyle.cancel){(cancle: UIAlertAction) -> Void in

         NotificationCenter.default.removeObserver(self)

      }

      alertController.addAction(okAction)

      alertController.addAction(cancelAction)

      self.present(alertController, animated: true, completion: nil)

      

   }

   func alertCodeTextFieldDidChange(_ notification: NSNotification){

      let alertController = self.presentedViewController as! UIAlertController

      let login = (alertController.textFields?.first)! as UITextField

      let okAction = alertController.actions.first! as UIAlertAction

      if login.text?.length == 4{

         okAction.isEnabled = true

      }else{

         okAction.isEnabled = false

      }

   }

   //短信验证码登陆 - 获得验证码

   func phoneCodeLogin(){

      dismissKeyboard()

      let alertController = UIAlertController(title: "提示",message:"请输入手机号",preferredStyle: UIAlertControllerStyle.alert)

      alertController.addTextField { (textField: UITextField) in

         textField.placeholder = "请输入手机号"

         NotificationCenter.default.addObserver(self, selector: #selector(self.alertTextFieldDidChange(_:)), name: NSNotification.Name.UITextFieldTextDidChange, object: textField)

      }

      let okAction = UIAlertAction(title: "下一步",style: UIAlertActionStyle.default){

         (action: UIAlertAction) -> Void in

         

         if let mobile = alertController.textFields?.first?.text{

            if mobile.isMobilePhoneNumber(){

               

               guard let timetamp = NSDate.getCurrentDate() else {

                  return

               }

               var parameters = [String:Any]()

               parameters["username"] = mobile

               parameters["timetamp"] = timetamp

               parameters["md5"] = ("hy" + mobile + timetamp).md5()

               parameters["code"] = Int(arc4random()%10000)

               

               HYClient.sharedInstance.appPostRequest("v2/reset/sms/send", parameters: parameters) { (obj, error) in

                  if let msg = obj?["msg"] as? String{

                     if msg == "验证码发送成功"{

                        NSToast.showText(msg, duration: NSToast.SHORT_DELAY)

                        self.phoneNumber = mobile

                        self.codeNumber()

                     }

                  }

               }

            }else{

               NSToast.showText("请输入正确的手机号码", duration: NSToast.SHORT_DELAY)

            }

         }

         NotificationCenter.default.removeObserver(self)

      }

      okAction.isEnabled = false

      let cancelAction = UIAlertAction(title: "取消",style: UIAlertActionStyle.cancel){(cancle: UIAlertAction) -> Void in

         NotificationCenter.default.removeObserver(self)

      }

      alertController.addAction(okAction)

      alertController.addAction(cancelAction)

      self.present(alertController, animated: true, completion: nil)

   }

   func alertTextFieldDidChange(_ notification: NSNotification){

      let alertController = self.presentedViewController as! UIAlertController

      let login = (alertController.textFields?.first)! as UITextField

      let okAction = alertController.actions.first! as UIAlertAction

      if login.text?.length == 11{

         okAction.isEnabled = true

      }else{

         okAction.isEnabled = false

      }

   }

转载于:https://www.cnblogs.com/linximu/p/6856854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值