Search code examples
iosswiftuipasteboarduttype

UIPasteboard setItems: options API, UTType.plainText: swift usage


I'm trying to use the following API in Swift: UIPasteboard: setItems(_:options:) , as suggested in this answer:

UIPasteboard.general.setItems([[kUTTypeUTF8PlainText as String: text]], options: [.expirationDate : Date(timeIntervalSinceNow: 10)])

However, it seems that the kUTTypeUTF8PlainText constant has been deprecated and I need to use the UniformTypeIdentifiers framework:

pasteboard.setItems([[String(UTType.plainText): string]],
                    options: [.expirationDate: Date(timeIntervalSinceNow: 120)])

However, the setItems: method accept only String type as the key, so I need to somehow convert the UTType.plainText to be of type String. How can I achieve this?


Solution

  • You should use the identifier property (also note the equivalent type is called utf8PlainText):

    UTType.utf8PlainText.identifier
    

    As the documentation says:

    API that doesn’t use UTType uses a String or CFString to refer to a type by its identifier.

    And UIPasteBoard is such an API.