Introduction
Security is becoming a bigger and bigger concern in the mobile space. As iOS developers, there are plenty of things we can do. We ensure sensitive information is saved in the keychain instead of plain text. We make sure content is encrypted before it's sent to a remote server. All this is done to make sure that the user's information is secure. Sometimes, however, we need to add an extra layer of protection at the user interface level.
Unless the user's device is enrolled in a mobile device management (MDM) solution, you cannot force your application's users to set up and use a passcode lock at the device level. ABPadLockScreen, however, provides a stylish, quick way to add such an interface to your iOS application. Let me show you how you can leverage ABPadLockScreen in your iOS applications.
1. Setup
ABPadLockScreen is available on GitHub, but I recommend installing it using CocoaPods. If you haven't started using CocoaPods for managing dependencies in your iOS and OS X projects, then you really should start today. It's the best way to manage dependancies in Cocoa projects. Since this tutorial isn't about CocoaPods I won’t go into the details of installing ABPadLockScreen using CocoaPods, but you can read plenty more about it at the CocoaPods website or read our introductory tutorial on Tuts+.
If you prefer to install ABPadLockScreen manually, then that's fine too. Download or clone the source code on GitHub and copy the files in the ABPadLockScreen folder into your Xcode project.
2. Pin Setup
The library includes two UIViewController
subclasses. The ABPadLockScreenSetupViewController
class is designed to allow the user to enter their initial pin. This is as simple as initializing a new instance of the view controller, passing a delegate, and presenting the view controller modally.
- (IBAction)setPin:(id)sender { ABPadLockScreenSetupViewController *lockScreen = [[ABPadLockScreenSetupViewController alloc] initWithDelegate:self pinLength:5]; lockScreen.modalPresentationStyle = UIModalPresentationFullScreen; lockScreen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentViewController:lockScreen animated:YES completion:nil]; }
The ABPadLockScreenSetupViewControllerDelegate
protocol has one required method, which is invoked when the pin is successfully set.
- (void)pinSet:(NSString *)pin padLockScreenSetupViewController:(ABPadLockScreenSetupViewController *)padLockScreenViewController;
3. Lock & Unlock
Setting up a pin isn't very useful unless the user gets a chance to enter it to gain access to the application. Once you’re ready to secure the application, all you need to do is present an instance of the ABPadLockScreenViewController
class, assign a delegate, a pin, and present he view controller modally.
- (IBAction)lockApp:(id)sender { ABPadLockScreenViewController *lockScreen = [[ABPadLockScreenViewController alloc] initWithDelegate:self pin:self.pin]; [lockScreen setAllowedAttempts:3]; lockScreen.modalPresentationStyle = UIModalPresentationFullScreen; lockScreen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentViewController:lockScreen animated:YES completion:nil]; }
If you set the allowedAttempts
property, the user will only have a predefined number of attempts before the module will lock them out. If allowedAttempts
is not set, then the user can try entering a pin as many times as she want.
The delegate of the ABPadLockScreenViewController
instance needs to conform to the ABPadLockScreenViewControllerDelegate
protocol, which declares four delegate methods.
- (void)unlockWasSuccessfulForPadLockScreenViewController:(ABPadLockScreenViewController *)padLockScreenViewController; - (void)unlockWasUnsuccessful:(NSString *)falsePin afterAttemptNumber:(NSInteger)attemptNumber padLockScreenViewController:(ABPadLockScreenViewController *)padLockScreenViewController; - (void)unlockWasCancelledForPadLockScreenViewController:(ABPadLockScreenViewController *)padLockScreenViewController; - (void)attemptsExpiredForPadLockScreenViewController:(ABPadLockScreenViewController *)padLockScreenViewController;
The methods are pretty self-explanatory. You can get a callback for a successful unlock, an unsuccessful entry, a cancellation—it that's allowed—, and if the user's attempts have expired reached the maximum allowed attempts.
4. Customization
There are several ways you can customize the interface of the lock screen and its behavior. You can:
- enable/disable the cancel button
- set the pin length, which is
4
by default
- set a custom text for any of the labels, which is useful for localization
- set the number of attempts, which default to
0
or an unlimited number of attempts
Aside from that, the user interface can also be customized very easily. The library uses the UIAppearance
API for customizing the user interface of the lock screen. Everything, from the background, text color, selection color, and fonts, can be set to match your application't design.
[[ABPadLockScreenView appearance] setLabelColour:[UIColor colorWithHexValue:@"DB4631"]]; [[ABPadLockScreenView appearance] setBackgroundColor:[UIColor colorWithHexValue:@“282B35”]];
Check out the view classes, ABPadLockScreenView
, ABPadButton
, and ABPinSelectionView
, to see what the view names are.
Conclusion
In this quick tip, we've briefly covered how to make your iOS application a little more secure by adding a lock screen to its user interface. I hope you find the library useful and easy to use. Happy coding.
Comments