Use of unresolved identifier 'kUTTypePDF'

You need to import import MobileCoreServices as "Robert Dresler" said

But you will see below error after import MobileCoreServices

'CFString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?

So you need to do kUTTypePDF as String

After that you may see an error in Data(contentsOf: URL(fileURLWithPath: path) as like below,

Call can throw, but it is not marked with 'try' and the error is not handled

So you need to use try and catch.

You final code will looks like below.

do{

        let data = try Data(contentsOf: URL(fileURLWithPath: path))

        UIPasteboard.general.setData(data, forPasteboardType: kUTTypePDF as String)

    }catch{

        print("error :\(error)")
    }

You have to import MobileCoreServices

import MobileCoreServices

Tags:

Swift