How to use the file types in Swift
according to https://developer.apple.com/documentation/coreservices/kuttypepdf
this should be completely fine
UIPasteboard.general.setData(Data(contentsOf: URL(fileURLWithPath: path)), forPasteboardType: kUTTypePDF)
yet it yelds
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)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With