Skip to content

Commit f2765ff

Browse files
authored
Auth jazzy fixes and Swift sample updates (#19)
* Removes the '@c' that would cause Jazzy issue down the road.
1 parent ed1b609 commit f2765ff

File tree

7 files changed

+347
-178
lines changed

7 files changed

+347
-178
lines changed

AuthSamples/SwiftSample/Main.storyboard

Lines changed: 188 additions & 148 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>application-identifier</key>
6+
<string>$(AppIdentifierPrefix)$(CFBundleIdentifier)</string>
7+
<key>aps-environment</key>
8+
<string>development</string>
9+
</dict>
10+
</plist>

AuthSamples/SwiftSample/ViewController.swift

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import UIKit
1919
import FirebaseDev // FirebaseAuth
2020
import GoogleSignIn // GoogleSignIn
2121

22-
final class ViewController: UIViewController {
22+
final class ViewController: UIViewController, UITextFieldDelegate {
2323
/// The profile image for the currently signed-in user.
2424
@IBOutlet weak var profileImage: UIImageView!
2525

@@ -56,6 +56,15 @@ final class ViewController: UIViewController {
5656
/// The "password" text field.
5757
@IBOutlet weak var passwordField: UITextField!
5858

59+
/// The "phone" text field.
60+
@IBOutlet weak var phoneField: UITextField!
61+
62+
/// The scroll view holding all content.
63+
@IBOutlet weak var scrollView: UIScrollView!
64+
65+
// The active keyboard input field.
66+
var activeField: UITextField?
67+
5968
/// The currently selected action type.
6069
fileprivate var actionType = ActionType(rawValue: 0)! {
6170
didSet {
@@ -86,6 +95,67 @@ final class ViewController: UIViewController {
8695
}
8796
}
8897

98+
func registerForKeyboardNotifications() {
99+
NotificationCenter.default.addObserver(self,
100+
selector:
101+
#selector(keyboardWillBeShown(notification:)),
102+
name: NSNotification.Name.UIKeyboardWillShow,
103+
object: nil)
104+
NotificationCenter.default.addObserver(self,
105+
selector: #selector(keyboardWillBeHidden(notification:)),
106+
name: NSNotification.Name.UIKeyboardWillHide,
107+
object: nil)
108+
}
109+
110+
func deregisterFromKeyboardNotifications() {
111+
NotificationCenter.default.removeObserver(self,
112+
name: NSNotification.Name.UIKeyboardWillShow,
113+
object: nil)
114+
NotificationCenter.default.removeObserver(self,
115+
name: NSNotification.Name.UIKeyboardWillHide,
116+
object: nil)
117+
}
118+
119+
func keyboardWillBeShown(notification: NSNotification) {
120+
scrollView.isScrollEnabled = true
121+
let info = notification.userInfo!
122+
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
123+
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
124+
125+
scrollView.contentInset = contentInsets
126+
scrollView.scrollIndicatorInsets = contentInsets
127+
128+
var aRect = self.view.frame
129+
aRect.size.height -= keyboardSize!.height
130+
if let activeField = activeField {
131+
if (!aRect.contains(activeField.frame.origin)) {
132+
scrollView.scrollRectToVisible(activeField.frame, animated: true)
133+
}
134+
}
135+
}
136+
137+
func keyboardWillBeHidden(notification: NSNotification){
138+
let info = notification.userInfo!
139+
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
140+
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
141+
scrollView.contentInset = contentInsets
142+
scrollView.scrollIndicatorInsets = contentInsets
143+
self.view.endEditing(true)
144+
scrollView.isScrollEnabled = false
145+
}
146+
147+
func textFieldDidBeginEditing(_ textField: UITextField) {
148+
activeField = textField
149+
}
150+
151+
func textFieldDidEndEditing(_ textField: UITextField) {
152+
activeField = nil
153+
}
154+
155+
func dismissKeyboard() {
156+
view.endEditing(true)
157+
}
158+
89159
/// The user's photo URL used by the last network request for its contents.
90160
fileprivate var lastPhotoURL: URL? = nil
91161

@@ -96,6 +166,15 @@ final class ViewController: UIViewController {
96166
object: Auth.auth(), queue: nil) { notification in
97167
self.updateUserInfo(notification.object as? Auth)
98168
}
169+
phoneField.delegate = self
170+
registerForKeyboardNotifications()
171+
172+
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
173+
scrollView.addGestureRecognizer(tap)
174+
}
175+
176+
override func viewWillDisappear(_ animated: Bool) {
177+
deregisterFromKeyboardNotifications()
99178
}
100179

101180
/// Executes the action designated by the operator on the UI.
@@ -200,7 +279,34 @@ final class ViewController: UIViewController {
200279
GIDSignIn.sharedInstance().signIn()
201280
case .password:
202281
completion(EmailAuthProvider.credential(withEmail: emailField.text!,
203-
password: passwordField.text!))
282+
password: passwordField.text!))
283+
case .phone:
284+
if #available(iOS 8.0, *) {
285+
let phoneNumber = phoneField.text
286+
PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber!) { verificationID, error in
287+
guard error == nil else {
288+
self.showAlert(title: "Error", message: error!.localizedDescription)
289+
return
290+
}
291+
292+
let codeAlertController =
293+
UIAlertController(title: "Enter Code", message: nil, preferredStyle: .alert)
294+
codeAlertController.addTextField { (textfield) in
295+
textfield.placeholder = "SMS Codde"
296+
textfield.keyboardType = UIKeyboardType.numberPad
297+
}
298+
codeAlertController.addAction(UIAlertAction(title: "OK",
299+
style: .default,
300+
handler: { (UIAlertAction) in
301+
let code = codeAlertController.textFields!.first!.text!
302+
let phoneCredential =
303+
PhoneAuthProvider.provider().credential(withVerificationID: verificationID ?? "",
304+
verificationCode: code)
305+
completion(phoneCredential)
306+
}))
307+
self.present(codeAlertController, animated: true, completion: nil)
308+
}
309+
}
204310
}
205311
}
206312

@@ -250,6 +356,7 @@ final class ViewController: UIViewController {
250356
action.requiresPassword
251357
passwordInputLabel.alpha = isPasswordEnabled ? 1.0 : 0.6
252358
passwordField.isEnabled = isPasswordEnabled
359+
phoneField.isEnabled = credentialType.requiresPhone
253360
}
254361

255362
fileprivate func showAlert(title: String, message: String? = "") {
@@ -486,11 +593,11 @@ fileprivate enum UserAction: Int, Action {
486593
/// The list of all possible credential types the operator can use to sign in or link.
487594
fileprivate enum CredentialType: Int {
488595

489-
case google, password
596+
case google, password, phone
490597

491598
/// Total number of enum values.
492599
static var count: Int {
493-
return CredentialType.password.rawValue + 1
600+
return CredentialType.phone.rawValue + 1
494601
}
495602

496603
/// The text description for a particular enum value.
@@ -500,6 +607,12 @@ fileprivate enum CredentialType: Int {
500607
return "Google"
501608
case .password:
502609
return "Password ➡️️"
610+
case .phone:
611+
if #available(iOS 8.0, *) {
612+
return "Phone ➡️️"
613+
} else {
614+
return "-"
615+
}
503616
}
504617
}
505618

@@ -512,6 +625,11 @@ fileprivate enum CredentialType: Int {
512625
var requiresPassword : Bool {
513626
return self == .password
514627
}
628+
629+
/// Whether or not the credential requires phone.
630+
var requiresPhone : Bool {
631+
return self == .phone
632+
}
515633
}
516634

517635
fileprivate extension User {

Firebase/Auth/Source/AuthProviders/Phone/FIRPhoneAuthProvider.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
#import "FIRAuth.h"
2019
#import "FIRAuthSwiftNameSupport.h"
2120

21+
@class FIRAuth;
2222
@class FIRPhoneAuthCredential;
2323

2424
NS_ASSUME_NONNULL_BEGIN
@@ -38,8 +38,8 @@ typedef void (^FIRVerificationResultCallback)(NSString *_Nullable verificationID
3838
NSError *_Nullable error)
3939
FIR_SWIFT_NAME(VerificationResultCallback);
4040

41-
/** @class FIRPhoneNumberProvider
42-
@brief A concrete implementation of @c FIRAuthProvider for Phone Auth Providers.
41+
/** @class FIRPhoneAuthProvider
42+
@brief A concrete implementation of @c FIRAuthProvider for phone auth providers.
4343
*/
4444
FIR_SWIFT_NAME(PhoneAuthProvider)
4545
@interface FIRPhoneAuthProvider : NSObject
@@ -52,7 +52,7 @@ FIR_SWIFT_NAME(PhoneAuthProvider)
5252
/** @fn providerWithAuth:
5353
@brief Returns an instance of @c FIRPhoneAuthProvider for the provided @c FIRAuth object.
5454
55-
@param auth The auth object to associate with the @c PhoneauthProvider instance.
55+
@param auth The auth object to associate with the phone auth provider instance.
5656
*/
5757
+ (instancetype)providerWithAuth:(FIRAuth *)auth FIR_SWIFT_NAME(provider(auth:));
5858

@@ -70,18 +70,18 @@ FIR_SWIFT_NAME(PhoneAuthProvider)
7070
@brief Creates an @c FIRAuthCredential for the phone number provider identified by the
7171
verification ID and verification code.
7272
73-
@param verificationID The verification ID obtained from invoking @c
73+
@param verificationID The verification ID obtained from invoking
7474
verifyPhoneNumber:completion:
7575
@param verificationCode The verification code obtained from the user.
76-
@return The corresponding @c FIRAuthCredential for the verification ID and verification code
76+
@return The corresponding phone auth credential for the verification ID and verification code
7777
provided.
7878
*/
7979
- (FIRPhoneAuthCredential *)credentialWithVerificationID:(NSString *)verificationID
8080
verificationCode:(NSString *)verificationCode;
8181

8282
/** @fn init
83-
@brief Please use the @c provider or @providerWithAuth: methods to obtain an instance of @c
84-
FIRPhoneAuthProvider.
83+
@brief Please use the @c provider or @c providerWithAuth: methods to obtain an instance of
84+
@c FIRPhoneAuthProvider.
8585
*/
8686
- (instancetype)init NS_UNAVAILABLE;
8787

Firebase/Auth/Source/FIRAuth.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ typedef void(^FIRIDTokenDidChangeListenerBlock)(FIRAuth *auth, FIRUser *_Nullabl
6262
/** @typedef FIRAuthDataResultCallback
6363
@brief The type of block invoked when sign-in related events complete.
6464
65-
@param authResult Optionally; Result of sign-in request containing @c FIRUser and
66-
@c FIRAdditionalUserInfo.
65+
@param authResult Optionally; Result of sign-in request containing both the user and
66+
the additional user info.
6767
@param error Optionally; the error which occurred - or nil if the request was successful.
6868
*/
6969
typedef void (^FIRAuthDataResultCallback)(FIRAuthDataResult *_Nullable authResult,
@@ -151,8 +151,8 @@ typedef void (^FIRApplyActionCodeCallback)(NSError *_Nullable error)
151151
FIR_SWIFT_NAME(ApplyActionCodeCallback);
152152

153153
/**
154-
@brief Keys used to retrieve operation data from a @c FIRActionCodeInfo object by the @c
155-
dataForKey method.
154+
@brief Keys used to retrieve operation data from a @c FIRActionCodeInfo object by the
155+
@c dataForKey method.
156156
*/
157157
typedef NS_ENUM(NSInteger, FIRActionDataKey) {
158158
/**
@@ -598,8 +598,8 @@ FIR_SWIFT_NAME(Auth)
598598
@brief Whether the specific remote notification is handled by @c FIRAuth .
599599
@param userInfo A dictionary that contains information related to the
600600
notification in question.
601-
@return Whether or the notification is handled. @c YES means the notification is for @c FIRAuth
602-
so the caller should ignore the notification from further processing, and @c NO means the
601+
@return Whether or the notification is handled. YES means the notification is for Firebase Auth
602+
so the caller should ignore the notification from further processing, and NO means the
603603
the notification is for the app (or another libaray) so the caller should continue handling
604604
this notification as usual.
605605
@remarks If swizzling is disabled, related remote notifications must be forwarded to this method

Firebase/Auth/Source/FIRAuthErrorUtils.m

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,20 +241,20 @@
241241
@brief Message for @c FIRAuthErrorCodeMissingVerificationCode error code.
242242
*/
243243
static NSString *const kFIRAuthErrorMessageMissingVerificationCode =
244-
@"The Phone Auth Credential was created with an empty SMS verification Code.";
244+
@"The phone auth credential was created with an empty SMS verification Code.";
245245

246246
/** @var kFIRAuthErrorMessageInvalidVerificationCode
247247
@brief Message for @c FIRAuthErrorCodeInvalidVerificationCode error code.
248248
*/
249249
static NSString *const kFIRAuthErrorMessageInvalidVerificationCode =
250250
@"The SMS verification code used to create the phone auth credential is invalid. Please resend "
251-
"the verification code sms and be sure use the verification code provided by the user.";
251+
"the verification code SMS and be sure to use the verification code provided by the user.";
252252

253253
/** @var kFIRAuthErrorMessageMissingVerificationID
254254
@brief Message for @c FIRAuthErrorCodeInvalidVerificationID error code.
255255
*/
256256
static NSString *const kFIRAuthErrorMessageMissingVerificationID =
257-
@"The Phone Auth Credential was created with an empty verification ID.";
257+
@"The phone auth credential was created with an empty verification ID.";
258258

259259
/** @var kFIRAuthErrorMessageInvalidVerificationID
260260
@brief Message for @c FIRAuthErrorCodeInvalidVerificationID error code.
@@ -280,13 +280,14 @@
280280
@brief Message for @c FIRAuthErrorCodeInvalidAppCredential error code.
281281
*/
282282
static NSString *const kFIRAuthErrorMessageInvalidAppCredential = @"The APNs device token provided "
283-
"may be incorrect or does not match the private certificate uploaded to the Firebase Console.";
283+
"is either incorrect or does not match the private certificate uploaded to the Firebase "
284+
"Console.";
284285

285286
/** @var kFIRAuthErrorMessageQuotaExceeded
286287
@brief Message for @c FIRAuthErrorCodeQuotaExceeded error code.
287288
*/
288-
static NSString *const kFIRAuthErrorMessageQuotaExceeded = @"The SMS quota for this project has "
289-
"been exceeded.";
289+
static NSString *const kFIRAuthErrorMessageQuotaExceeded = @"The phone verification quota for this "
290+
"project has been exceeded.";
290291

291292
/** @var kFIRAuthErrorMessageMissingAppToken
292293
@brief Message for @c FIRAuthErrorCodeMissingAppToken error code.

Firebase/Auth/Source/FIRAuthErrors.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ typedef NS_ENUM(NSInteger, FIRAuthErrorCode) {
128128
FIRAuthErrorCodeInvalidUserToken = 17017,
129129

130130
/** Indicates a network error occurred (such as a timeout, interrupted connection, or
131-
unreachable host). These types of errors are often recoverable with a retry. The @c
132-
NSUnderlyingError field in the @c NSError.userInfo dictionary will contain the error
131+
unreachable host). These types of errors are often recoverable with a retry. The
132+
@c NSUnderlyingError field in the @c NSError.userInfo dictionary will contain the error
133133
encountered.
134134
*/
135135
FIRAuthErrorCodeNetworkError = 17020,
@@ -187,13 +187,13 @@ typedef NS_ENUM(NSInteger, FIRAuthErrorCode) {
187187
// The enum values between 17033 and 17041 are reserved and should NOT be used for new error
188188
// codes.
189189

190-
/** Indicates that a phone number was not provided in a call to @c
191-
verifyPhoneNumber:completion:.
190+
/** Indicates that a phone number was not provided in a call to
191+
@c verifyPhoneNumber:completion:.
192192
*/
193193
FIRAuthErrorCodeMissingPhoneNumber = 17041,
194194

195-
/** Indicates that an invalid phone number was provided in a call to @c
196-
verifyPhoneNumber:completion:.
195+
/** Indicates that an invalid phone number was provided in a call to
196+
@c verifyPhoneNumber:completion:.
197197
*/
198198
FIRAuthErrorCodeInvalidPhoneNumber = 17042,
199199

0 commit comments

Comments
 (0)