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
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
2. Wipe the content in the paste board in the AppDelegate's applicationWillResignActive method
UIPasteboard.general.items = [[String: Any]()]
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)
}
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
}
}
}
This will store your app's data to custom paste board.
Apple Doc of UIPasteboard
Happy Coding🔥
Top comments (1)
👍