Securing Your iOS App with ABPadLockScreen

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.

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.

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.

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.

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.