Protect Copied Text, iOS
Safeguarding Confidential Information with Custom Encryption Techniques
In the world of mobile apps, keeping user data private and secure is crucial. Copying and pasting text is a common action, but what if we could protect copied text from being read by other apps? In this story, we’ll explore how we can achieve this using the iOS UIPasteboard.
- Creating a Custom Text Field
To control the actions allowed within the text field, we’ll create a special text field called FTextField. It will only permit copying and pasting actions, ensuring a higher level of control.
class FTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
let validActions: [Selector] = [
#selector(copy(_:)),
#selector(paste(_:)),
]
return validActions.contains(action)
}
override func paste(_ sender: Any?) {
if let text = AppPasteboard.general.copiedText {
self.replace(self.selectedTextRange ?? .init(), withText: text)
return
}
super.paste(sender)
}
}2. Encrypting the Copied Text
Get Mukhammadjon Tokhirov’s stories in your inbox
Join Medium for free to get updates from this writer.
To protect the copied text, we’ll use the AppPasteboard class. It generates a secret key and encrypts the text with it. When the pasteboard changes, we'll check if the new content is already encrypted. If not, we'll encrypt it using the secret key and update the pasteboard.
final class AppPasteboard {
static let general: AppPasteboard = .init()
lazy private var pasteboardPrivateKey = {UUID()}()
func setup() {
NotificationCenter.default.addObserver(self, selector: #selector(pasteboardChanged), name: UIPasteboard.changedNotification, object: nil)
}
@objc private func pasteboardChanged(notification: Notification) {
if let pasteboardString = UIPasteboard.general.string, !pasteboardString.starts(with: pasteboardPrivateKey.uuidString) {
encrypt(pasteboardString)
}
}
private func encrypt(_ text: String) {
if let encText = text.asBase64 {
UIPasteboard.general.string = pasteboardPrivateKey.uuidString + encText
}
}
private func decrypt(_ text: String) -> String? {
if let realEncrypted = text.replacingOccurrences(of: pasteboardPrivateKey.uuidString, with: "").fromBase64 {
return realEncrypted
}
return nil
}
var copiedText: String? {
if let str = UIPasteboard.general.string, str.starts(with: pasteboardPrivateKey.uuidString) {
return decrypt(str)
}
return UIPasteboard.general.string
}
}3. Enabling Encryption and Decryption with String Extensions
This extension provides additional methods specifically designed for encrypting and decrypting text. You can modify these methods according to your preferences and specific requirements.
extension String {
var asBase64: String? {
if let data = self.data(using: .utf16) {
let base64String = data.base64EncodedString()
return base64String
}
return nil
}
var fromBase64: String? {
if let data = Data(base64Encoded: self) {
let base64Decoded = String(data: data, encoding: .utf16)
return base64Decoded
}
return nil
}
}4. Simple usage
class ViewController: UIViewController {
@IBOutlet weak var textField: FTextField!
override func viewDidLoad() {
super.viewDidLoad()
AppPasteboard.general.setup()
}
}By using a custom text field and the iOS UIPasteboard, we’ve created a way to protect copied text on iOS. The custom text field ensures that only copying and pasting actions are allowed, giving us control over the user’s actions. The AppPasteboard class encrypts the copied text, making it unreadable to other apps. By decrypting the text using the secret key, we can access the original content when needed.
Thank you for reading until the end. Please consider following the writer and this publication. Visit Stackademic to find out more about how we are democratizing free programming education around the world.











