A.I, Data and Software Engineering

Don’t copy and paste sensitive information – here is why

D

Clipboard was created for productivity purposes but developers can easily exploit this for malicious purposes. This article will demonstrates how to access Android/iOS clipboard from an activity or background service.

Clipboard access

Android provides a powerful clipboard-based framework for copying and pasting. It supports both simple and complex data types, including text strings, complex data structures, text and binary stream data, and even application assets. Simple text data is stored directly in the clipboard, while complex data is stored as a reference that the pasting application resolves with a content provider. 

A block diagram of the copy and paste framework
The Android clipboard framework

To copy data, an application puts a ClipData object on the ClipboardManager global clipboard. The ClipData contains one or more ClipData.Item objects and one ClipDescription object. To paste data, an application gets the ClipData, gets its MIME type from the ClipDescription, and gets the data either from the ClipData.Item or from the content provider referred to by ClipData.Item.

To get the data from the clipboard is rather easy.

Nevertheless, it is only available in App (from Android 10+), and when the activity has the focus. So you can implement the code in the onWindowFocusChanged method.

Access from a background service.

Although it is now not possible to access clipboard outside of the current app context, sometimes, you may want it to run from the background (without UI). We can follow these steps:

  • Create a service
  • Implement onStartCommand

The code for the service can be as simple as this:

iOS: copy text to the clipboard (SWIFT)

You can write to and read from the iOS clipboard by using the UIPasteboard class, which has a generalPasteboard() method that returns the shared system method of copying and pasting data between apps. Using this you can write text to the clipboard just like this:

To read text back from the clipboard, you should unwrap its optional value like this:

Video tutorial

Useful links

  • Subscribe to Petamind youtube channel
  • For other projects using Kotlin, please click here.

Add comment

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Pin It on Pinterest

Newsletters

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.