Saturday, March 9, 2019

Copy, Cut, and Paste Operations using Swift

Copy a string to the pasteboard


1). Write data to the pasteboard programmatically
    
UIPasteboard.general.setValue("hello", forPasteboardType: "public.plain-text")
 
2). Open another app for example, Google Chrome browser, and touch the address bar twice to show the edit menu (copy, cut, and paste commands). Then, touch Paste.


Concepts


Pasteboard


A pasteboard is an area for storing data shared within an app or between apps. There are two types of pasteboard: public/system and private/app. We can access the system pasteboard through the type property general of UIPasteboard class. The system pasteboard is shared among apps. The data written to the pasteboard by the previous app get overridden by the data written by the current app. There may be one or two items stored on the pasteboard. An item might group one or more types of the data. We should store multiple types of data so that it's compatible with many apps.


UTI (Uniform Type Identifier)


UTI is a string to identify a class of entities (documents, pasteboard data, and bundles). UTI replaces the incompatible existing methods of tagging data. Currently, for example, a JPEG file is identified by any of the following methods:
1. A four character type code of JPEG
2. A filename extension of .jpg
3. A filename extension of .jpeg
4. A MIME type of image/jpeg
With UTI, the data is identified by the string public.jpeg. Then, we can use a utility function to translate from one to the other.

The key advantage of UTI over the other tagging methods is a conformance hierarchy. For example, the UTI public.html, which defines HTML text, conforms to the base text identifier, public.text. In this case, conformance lets applications which can open the general text files identify HTML files as ones i can open as well.





References
https://developer.apple.com/library/archive/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/UsingCopy,Cut,andPasteOperations/UsingCopy,Cut,andPasteOperations.html#//apple_ref/doc/uid/TP40009542-CH11-SW21
https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html#//apple_ref/doc/uid/TP40001319-CH202-CHDHIJDE


No comments:

Post a Comment