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
}
}