DEV Community

Bibin Jaimon
Bibin Jaimon

Posted on

#1 iOS Security Tips: UIPasteboard | iOS Development

What is UIPasteboard:

This is a method to share data between any other app using cut or copy methods.

How it become a Security concern:

When we copy a sensitive piece of data, It will store in the general paste board of our system which can access by another apps

How to access general paste board programmatically:

UIPasteBoard.general.string
Enter fullscreen mode Exit fullscreen mode

Methods to prevent this security issue:

1. isSecureTextEntry

UITextField has a property called isSecureTextEntry. We can set this to true. Its mainly used for passwords

let passwordTextField = UITextField()
passwordTextField.isSecureTextEntry = true
Enter fullscreen mode Exit fullscreen mode

2. Wipe the content in the paste board in the AppDelegate's applicationWillResignActive method

UIPasteboard.general.items = [[String: Any]()]
Enter fullscreen mode Exit fullscreen mode

3. Create an app specific paste board

The data will not be available to other app if we use custom paste board.
How to create a custom paste board in AppDelegate

extension AppDelegate {
    static let pastboardName = UIPasteboard.Name(rawValue: "CustomPasteBoard")
    static var customPasteBoard: UIPasteboard? = UIPasteboard(name: pastboardName, create: true)
}
Enter fullscreen mode Exit fullscreen mode

How to store data in custom paste board

extension UITextField {
    open override func copy(_ sender: Any?) {
        AppDelegate.customPasteBoard?.string = self.text
    }

    open override func cut(_ sender: Any?) {
        AppDelegate.customPasteBoard?.string = self.text
        self.text = nil
    }

    open override func paste(_ sender: Any?) {
        if let text = AppDelegate.customPasteBoard?.string {
            self.text = text
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This will store your app's data to custom paste board.

Apple Doc of UIPasteboard
Happy Coding🔥

Top comments (1)

Collapse
 
akhil_karonnon_ profile image
Akhil Karonnon

👍