Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions PhoneAuth/FirebasePhoneAuthUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -13,7 +13,6 @@
8D69E5D221DE91D400CFA49B /* FUIPhoneAuthStrings.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D69E55B21DE91D200CFA49B /* FUIPhoneAuthStrings.m */; };
8D69E5D321DE91D400CFA49B /* FUIPrivacyAndTermsOfServiceView+PhoneAuth.h in Headers */ = {isa = PBXBuildFile; fileRef = 8D69E55C21DE91D200CFA49B /* FUIPrivacyAndTermsOfServiceView+PhoneAuth.h */; };
8D69E5D421DE91D400CFA49B /* FirebasePhoneAuthUI.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8D69E55E21DE91D200CFA49B /* FirebasePhoneAuthUI.strings */; };
8D69E5D521DE91D400CFA49B /* FUICodeField.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8D69E5B321DE91D300CFA49B /* FUICodeField.xib */; };
8D69E5D621DE91D400CFA49B /* ic_phone.png in Resources */ = {isa = PBXBuildFile; fileRef = 8D69E5B521DE91D300CFA49B /* ic_phone.png */; };
8D69E5D721DE91D400CFA49B /* ic_phone@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = 8D69E5B621DE91D300CFA49B /* ic_phone@3x.png */; };
8D69E5D821DE91D400CFA49B /* ic_phone@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 8D69E5B721DE91D300CFA49B /* ic_phone@2x.png */; };
Expand Down Expand Up @@ -147,7 +146,6 @@
8D69E5B021DE91D200CFA49B /* ca */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ca; path = ca.lproj/FirebasePhoneAuthUI.strings; sourceTree = "<group>"; };
8D69E5B121DE91D200CFA49B /* zh */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = zh; path = zh.lproj/FirebasePhoneAuthUI.strings; sourceTree = "<group>"; };
8D69E5B221DE91D200CFA49B /* en-IN */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "en-IN"; path = "en-IN.lproj/FirebasePhoneAuthUI.strings"; sourceTree = "<group>"; };
8D69E5B321DE91D300CFA49B /* FUICodeField.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FUICodeField.xib; sourceTree = "<group>"; };
8D69E5B521DE91D300CFA49B /* ic_phone.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ic_phone.png; sourceTree = "<group>"; };
8D69E5B621DE91D300CFA49B /* ic_phone@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ic_phone@3x.png"; sourceTree = "<group>"; };
8D69E5B721DE91D300CFA49B /* ic_phone@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ic_phone@2x.png"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -229,7 +227,6 @@
8D69E54421DE915A00CFA49B /* FirebasePhoneAuthUI.h */,
8D69E5D021DE91D300CFA49B /* FUICodeField.h */,
8D69E5BB21DE91D300CFA49B /* FUICodeField.m */,
8D69E5B321DE91D300CFA49B /* FUICodeField.xib */,
8D69E5CC21DE91D300CFA49B /* FUIPhoneAuth_Internal.h */,
8D69E5B921DE91D300CFA49B /* FUIPhoneAuth.h */,
8D69E5CF21DE91D300CFA49B /* FUIPhoneAuth.m */,
Expand Down Expand Up @@ -485,7 +482,6 @@
8D69E5D821DE91D400CFA49B /* ic_phone@2x.png in Resources */,
8D69E5E221DE91D400CFA49B /* FUICountryTableViewController.xib in Resources */,
8D69E5D621DE91D400CFA49B /* ic_phone.png in Resources */,
8D69E5D521DE91D400CFA49B /* FUICodeField.xib in Resources */,
8D69E5EB21DE91D400CFA49B /* FUIPhoneEntryViewController.xib in Resources */,
8D69E5D921DE91D400CFA49B /* country-codes.json in Resources */,
8D69E5D721DE91D400CFA49B /* ic_phone@3x.png in Resources */,
Expand Down
12 changes: 2 additions & 10 deletions PhoneAuth/FirebasePhoneAuthUI/FUICodeField.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,14 @@ NS_ASSUME_NONNULL_BEGIN

@end

@interface FUICodeField : UIView <UIKeyInput, UITextInputTraits>

@property (nonatomic, strong) UIColor *textColor UI_APPEARANCE_SELECTOR;

@property (nonatomic, assign) UIKeyboardAppearance keyboardAppearance UI_APPEARANCE_SELECTOR;
@interface FUICodeField : UITextField <UITextFieldDelegate>

@property (nonatomic, retain, readonly) NSMutableString *codeEntry;

@property (nonatomic,getter=isSecureTextEntry) IBInspectable BOOL secureTextEntry;

@property (nonatomic, readwrite) IBOutlet id<FUICodeFieldDelegate> delegate;
@property (nonatomic, readwrite) IBOutlet id<FUICodeFieldDelegate> codeDelegate;

@property (nonatomic, readonly) IBInspectable NSInteger codeLength;

@property (null_unspecified, nonatomic, copy) UITextContentType textContentType;

- (void)clearCodeInput;

@end
Expand Down
135 changes: 49 additions & 86 deletions PhoneAuth/FirebasePhoneAuthUI/FUICodeField.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,37 @@

@interface FUICodeField ()

@property (nonatomic, retain, readonly) UIView *inputField;

@property (weak, nonatomic) IBOutlet UILabel *digits;

@property (nonatomic, readonly) IBInspectable NSString *placeholder;
@property (nonatomic, readonly) IBInspectable NSString *placeholderChar;

@end

@implementation FUICodeField

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]){
[self setUpFromNib];
[self commonInit];
}
return self;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]){
[self setUpFromNib];
[self commonInit];
}
return self;
}

- (void)setUpFromNib {
NSBundle *bundle = [FUIAuthUtils bundleNamed:FUIPhoneAuthBundleName];
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:bundle];

_inputField = [nib instantiateWithOwner:self options:nil][0];
self.inputField.frame = [self bounds];
self.inputField.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.inputField.userInteractionEnabled = YES;

if (@available(iOS 12.0, *)) {
if ([self.inputField respondsToSelector:@selector(setTextContentType:)]) {
id<UITextInputTraits> inputField = (id<UITextInputTraits>)self.inputField;
inputField.textContentType = UITextContentTypeOneTimeCode;
}
}

- (void)commonInit {
// Initialization code
_codeEntry = [NSMutableString string];
self.backgroundColor = UIColor.clearColor;
self.tintColor = UIColor.clearColor;
self.font = [UIFont fontWithName:@"Courier" size:40];
self.textAlignment = NSTextAlignmentLeft;
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, self.bounds.size.height)];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cursor in UITextField made me increase the size of the area for the codeField in the FUIPhoneVerificationViewController.xib and add this space here

self.leftView = paddingView;
self.leftViewMode = UITextFieldViewModeAlways;
self.textContentType = UITextContentTypeOneTimeCode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the if (@available(iOS 12.0, *)) check around this line since it's only available on iOS 12 and higher.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll accept the PR as is and add this line manually since I want to get it into the next release. Thanks @akivab!


// Default values
if (!self.codeLength) {
Expand All @@ -76,11 +64,12 @@ - (void)setUpFromNib {
_codeLength = MIN(self.codeLength, 12);
}

if (!self.placeholder || !self.placeholder.length) {
_placeholder = @"-";
if (!self.placeholderChar || !self.placeholderChar.length) {
_placeholderChar = @"-";
}

[self addSubview:self.inputField];
self.delegate = self;
[self updateText];
}

- (UIKeyboardType) keyboardType {
Expand All @@ -91,38 +80,6 @@ - (UIKeyboardType) keyboardType {
}
}

- (BOOL)canBecomeFirstResponder {
return YES;
}

- (void) touchesBegan: (NSSet *) touches withEvent: (nullable UIEvent *) event {
[self becomeFirstResponder];
}

- (void)drawRect:(CGRect)rect {
NSString *code = [self.codeEntry copy];
if (self.secureTextEntry) {
code = [[NSString string] stringByPaddingToLength:code.length
withString:@"\u2022" startingAtIndex:0];
}

NSInteger add = self.codeLength - code.length;
if (add > 0) {
NSString *pad = [[NSString string] stringByPaddingToLength:add
withString:self.placeholder
startingAtIndex:0];
code = [code stringByAppendingString:pad];
}

NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:code];
[attributedString addAttribute:NSKernAttributeName value:@20
range:NSMakeRange(0, attributedString.length-1)];

self.digits.text = @"";
[self.digits setAttributedText:attributedString];
}

- (BOOL)hasText {
return self.codeEntry.length > 0;
}
Expand All @@ -131,63 +88,69 @@ - (void)insertText:(NSString *)theText {
if (self.codeEntry.length >= self.codeLength){
// UX: if code was submitted and there is an error message,
// typing a new number should clear the field and start over
[self updateText];
return;
}

[self.codeEntry appendString:theText];
[self setNeedsDisplay];
[self updateText];
[self notifyEntryCompletion];
}

- (void)deleteBackward {
if (!self.codeEntry.length){
if (!self.codeEntry.length) {
return;
}

NSRange theRange = NSMakeRange(self.codeEntry.length-1, 1);
NSRange theRange = NSMakeRange(self.codeEntry.length - 1, 1);
[self.codeEntry deleteCharactersInRange:theRange];
[self setNeedsDisplay];
[self updateText];
[self notifyEntryCompletion];
}

- (void)clearCodeInput {
[self.codeEntry setString:@""];

[self setNeedsDisplay];
[self updateText];
[self notifyEntryCompletion];
}

- (void)notifyEntryCompletion {
if (self.codeEntry.length >= self.codeLength) {
[self.delegate entryIsCompletedWithCode:[self.codeEntry copy]];
[self.codeDelegate entryIsCompletedWithCode:[self.codeEntry copy]];
} else {
[self.delegate entryIsIncomplete];
[self.codeDelegate entryIsIncomplete];
}
}

- (CGSize)inputFieldIntrinsicContentSize {
CGSize textFieldSize = [self.inputField intrinsicContentSize];
if (textFieldSize.height < FUICodeFieldMinInputFieldHeight) {
textFieldSize.height = FUICodeFieldMinInputFieldHeight;
}

return textFieldSize;
}
- (void)updateText {
NSString *code = [self.codeEntry copy];
if (self.secureTextEntry) {
code = [[NSString string] stringByPaddingToLength:code.length
withString:@"\u2022" startingAtIndex:0];
}

- (CGSize)intrinsicContentSize {
CGSize textFieldSize = [self inputFieldIntrinsicContentSize];
return textFieldSize;
}
NSInteger add = self.codeLength - code.length;
if (add > 0) {
NSString *pad = [[NSString string] stringByPaddingToLength:add
withString:self.placeholderChar
startingAtIndex:0];
code = [code stringByAppendingString:pad];
}

- (UITextContentType _Null_unspecified)textContentType {
if (@available(iOS 12.0, *)) {
return UITextContentTypeOneTimeCode;
}
return nil;
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:code];
[attributedString addAttribute:NSKernAttributeName value:@20
range:NSMakeRange(0, attributedString.length - 1)];
self.attributedText = attributedString;
}

- (void)setTextContentType:(_Null_unspecified UITextContentType)textContentType {
// do nothing
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (string.length == 0) {
[self deleteBackward];
} else {
[self insertText:string];
}
return false;
}

@end
Expand Down
42 changes: 0 additions & 42 deletions PhoneAuth/FirebasePhoneAuthUI/FUICodeField.xib

This file was deleted.

Loading