- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy text to the clipboard/pasteboard with Swift?
In Swift, there is a dedicated class called UIPasteboard that allows you to copy text to the pasteboard. The same class allows you to paste the text.
A UIPasteboard class is part of the UIKit framework that provides a general processor for copying and pasting information in iOS applications. In this class, data can be copied and pasted between apps using a shared instance. You can share various types of information such as text, media files, URLs, and colors.
Copy and Paste the Text to the Clipboard
Using the UIPasteboard class, you can copy and paste the text value in the iOS app via the string property. Here is an example −
Example
import UIKit let text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book." UIPasteboard.general.string = text
This will copy the text string to the general pasteboard, which can be accessed by other apps.
In order to paste the text from the pasteboard, you can retrieve it like this −
import UIKit let text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book." UIPasteboard.general.string = text if let pastedText = UIPasteboard.general.string { print("Pasted text: \(pastedText)") }
Output
Pasted text: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
This string property gives you the current text in the general pasteboard if any, that's why it returns an optional string value.
Copy and Paste the URL to the Clipboard
Using the UIPasteboard class, you can copy and paste a URL representation in the iOS app via the "url" property. Here is an example −
Example
let urlObject = URL(string: "https://www.tutorialspoint.com/") UIPasteboard.general.url = urlObject if let pastedObject = UIPasteboard.general.url { print("Pasted value: \(pastedObject)") }
Output
Pasted value: https://www.tutorialspoint.com/
Note − Starting in iOS 14, the system notifies the user when an app gets general pasteboard content that originates in a different app without user intent. The system determines user intent based on user interactions, such as tapping a system-provided control or pressing Command-V. Use the properties and methods below to determine whether pasteboard items match various patterns, such as web search terms, URLs, or numbers, without notifying the user.
Conclusion
By using the shared instance of UIPasteboard, you can share text, images, URLs, and other types of information. There is a shared instance called "general" in this class that is used to perform all general copying and pasting operations.