From 6beb119a856441ee742fbec6cbf84f85ba3eae04 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Tue, 17 Mar 2015 22:57:49 +0800 Subject: [PATCH] add YepAlert; show errorMessage when sendVerifyCode failed --- Yep.xcodeproj/project.pbxproj | 12 ++++++ Yep/Helpers/YepAlert.swift | 32 +++++++++++++++ Yep/Services/YepService.swift | 2 +- .../Login/LoginByMobileViewController.swift | 40 ++++++++++++++----- 4 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 Yep/Helpers/YepAlert.swift diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 4cadb1d7..093146e0 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -39,6 +39,7 @@ 502AE5331AB86020005BD199 /* RegisterVerifyMobileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 502AE5321AB86020005BD199 /* RegisterVerifyMobileViewController.swift */; }; 502AE5351AB8649F005BD199 /* LoginByMobileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 502AE5341AB8649F005BD199 /* LoginByMobileViewController.swift */; }; 502AE5381AB866E3005BD199 /* LoginVerifyMobileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 502AE5371AB866E3005BD199 /* LoginVerifyMobileViewController.swift */; }; + 502AE53A1AB871C1005BD199 /* YepAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 502AE5391AB871C1005BD199 /* YepAlert.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -88,6 +89,7 @@ 502AE5321AB86020005BD199 /* RegisterVerifyMobileViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RegisterVerifyMobileViewController.swift; path = ViewControllers/Register/RegisterVerifyMobileViewController.swift; sourceTree = ""; }; 502AE5341AB8649F005BD199 /* LoginByMobileViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LoginByMobileViewController.swift; path = ViewControllers/Login/LoginByMobileViewController.swift; sourceTree = ""; }; 502AE5371AB866E3005BD199 /* LoginVerifyMobileViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LoginVerifyMobileViewController.swift; path = ViewControllers/Login/LoginVerifyMobileViewController.swift; sourceTree = ""; }; + 502AE5391AB871C1005BD199 /* YepAlert.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepAlert.swift; path = Helpers/YepAlert.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -131,6 +133,7 @@ children = ( 5023DE441AB6B19300B3EE96 /* DummyData */, 0A8F54B01AB67D11004AD60E /* AppDelegate.swift */, + 502AE53B1AB871C5005BD199 /* Helpers */, 502AE52C1AB819B5005BD199 /* Configs */, 502AE5251AB72BE9005BD199 /* Services */, 5023DE571AB6CA1400B3EE96 /* Caches */, @@ -334,6 +337,14 @@ name = Login; sourceTree = ""; }; + 502AE53B1AB871C5005BD199 /* Helpers */ = { + isa = PBXGroup; + children = ( + 502AE5391AB871C1005BD199 /* YepAlert.swift */, + ); + name = Helpers; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -451,6 +462,7 @@ 5023DE591AB6DBC100B3EE96 /* UIImage+Yep.swift in Sources */, 502AE5331AB86020005BD199 /* RegisterVerifyMobileViewController.swift in Sources */, 5023DE2D1AB685ED00B3EE96 /* DiscoverViewController.swift in Sources */, + 502AE53A1AB871C1005BD199 /* YepAlert.swift in Sources */, 502AE52E1AB83879005BD199 /* UnderLineTextField.swift in Sources */, 5023DE301AB6861500B3EE96 /* ProfileViewController.swift in Sources */, 5023DE261AB6856300B3EE96 /* ConversationsViewController.swift in Sources */, diff --git a/Yep/Helpers/YepAlert.swift b/Yep/Helpers/YepAlert.swift new file mode 100644 index 00000000..96c7ce53 --- /dev/null +++ b/Yep/Helpers/YepAlert.swift @@ -0,0 +1,32 @@ +// +// YepAlert.swift +// Yep +// +// Created by NIX on 15/3/17. +// Copyright (c) 2015年 Catch Inc. All rights reserved. +// + +import UIKit + +class YepAlert { + class func alert(#title: String, message: String, inViewController viewController: UIViewController, withDismissAction dismissAction: (() -> Void)?) { + let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) + + let action: UIAlertAction = UIAlertAction(title: NSLocalizedString("Dismiss", comment: ""), style: .Default) { action -> Void in + if let dismissAction = dismissAction { + dismissAction() + } + } + alertController.addAction(action) + + viewController.presentViewController(alertController, animated: true, completion: nil) + } + + class func alertSorry(#message: String, inViewController viewController: UIViewController, withDismissAction dismissAction: () -> Void) { + alert(title: NSLocalizedString("Sorry", comment: ""), message: message, inViewController: viewController, withDismissAction: dismissAction) + } + + class func alertSorry(#message: String, inViewController viewController: UIViewController) { + alert(title: NSLocalizedString("Sorry", comment: ""), message: message, inViewController: viewController, withDismissAction: nil) + } +} \ No newline at end of file diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index e83d032b..5b339eb7 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -22,7 +22,7 @@ func errorMessageInData(data: NSData?) -> String? { return nil } -func sendVerifyCode(ofMobile mobile: String, withAreaCode areaCode: String, #failureHandler: ((Resource, Reason, NSData?) -> ())?, completion: Bool -> ()) { +func sendVerifyCode(ofMobile mobile: String, withAreaCode areaCode: String, #failureHandler: ((Resource, Reason, NSData?) -> ())?, #completion: Bool -> ()) { let requestParameters = [ "mobile": mobile, diff --git a/Yep/ViewControllers/Login/LoginByMobileViewController.swift b/Yep/ViewControllers/Login/LoginByMobileViewController.swift index a9afdac1..32d687a9 100644 --- a/Yep/ViewControllers/Login/LoginByMobileViewController.swift +++ b/Yep/ViewControllers/Login/LoginByMobileViewController.swift @@ -54,26 +54,48 @@ class LoginByMobileViewController: UIViewController { @IBAction func next(sender: UIButton) { - showLoginVerifyMobile() + tryShowLoginVerifyMobile() } - private func showLoginVerifyMobile() { + private func tryShowLoginVerifyMobile() { + + view.endEditing(true) let mobile = mobileNumberTextField.text let areaCode = areaCodeTextField.text - sendVerifyCode(ofMobile: mobile, withAreaCode: areaCode, failureHandler: nil) { success in - if success { - println("Verification code sent successfully") + sendVerifyCode(ofMobile: mobile, withAreaCode: areaCode, failureHandler: { (resource, reason, data) in + defaultFailureHandler(forResource: resource, withFailureReason: reason, data) + if let errorMessage = errorMessageInData(data) { dispatch_async(dispatch_get_main_queue(), { () -> Void in - self.performSegueWithIdentifier("showLoginVerifyMobile", sender: ["mobile" : mobile, "areaCode": areaCode]) + YepAlert.alertSorry(message: errorMessage, inViewController: self, withDismissAction: { () -> Void in + mobileNumberTextField.becomeFirstResponder() + }) + }) + } + + }, completion: { success in + if success { + dispatch_async(dispatch_get_main_queue(), { () -> Void in + self.showLoginVerifyMobile() }) } else { - println("Failed to send verification code") + dispatch_async(dispatch_get_main_queue(), { () -> Void in + YepAlert.alertSorry(message: NSLocalizedString("Failed to send verification code", comment: ""), inViewController: self, withDismissAction: { () -> Void in + mobileNumberTextField.becomeFirstResponder() + }) + }) } - } + }) + } + + func showLoginVerifyMobile() { + let mobile = mobileNumberTextField.text + let areaCode = areaCodeTextField.text + + self.performSegueWithIdentifier("showLoginVerifyMobile", sender: ["mobile" : mobile, "areaCode": areaCode]) } // MARK: Navigation @@ -95,7 +117,7 @@ class LoginByMobileViewController: UIViewController { extension LoginByMobileViewController: UITextFieldDelegate { func textFieldShouldReturn(textField: UITextField) -> Bool { if !textField.text.isEmpty { - showLoginVerifyMobile() + tryShowLoginVerifyMobile() } return true