Reputation: 31
I want to share pdf files from application. I get an error "Not able to share" document some pdfs. I do not get an error in someFiles. Here is my code :
class DocumentURL: UIActivityItemProvider {
let temporaryURL: URL
let document: DocumentAndDemandForm
let data : Data
init(document: DocumentAndDemandForm,data:Data) {
self.document = document
self.data = data
self.temporaryURL = URL(fileURLWithPath: NSTemporaryDirectory() + "\(document.downloadName == "" ? document.description ?? "ek" : document.downloadName).pdf")
super.init(placeholderItem: temporaryURL)
}
override var item: Any {
get {
try? data.write(to: temporaryURL)
return temporaryURL
}
}
}
func share(isSave:Bool = false) {
if let data = pdfView.document?.dataRepresentation(){
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [DocumentURL.init(document: self.document, data: data)], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.pdfView
if isSave{
activityViewController.excludedActivityTypes = [.airDrop,.assignToContact,.copyToPasteboard,.mail,.message,.openInIBooks,.postToFacebook,.postToFlickr,.postToTencentWeibo,.postToTwitter,.postToVimeo,.postToWeibo]
}
DispatchQueue.main.async {
self.controller.present(activityViewController, animated: true, completion: {
self.shareDocument = false
})
}
}
}
Upvotes: 1
Views: 801
Reputation: 91
Add these two keys in your Info.plist
:
UIFileSharingEnabled
: this key (Application Supports iTunes file sharing) will enable iTunes sharing of files in your Documents folder. Make sure to set the Boolean value to true.LSSupportsOpeningDocumentsInPlace
: this Key (Support opening documents in place) will give access to the application file provider to read or write files in the Documents Folder. Make sure you set the Boolean value to YES.let fileManager = FileManager.default
let documentoPath = getDirectoryPath().appendingPathComponent(url.lastPathComponent) as URL
print("documentoPath :\(documentoPath)")
if fileManager.fileExists(atPath: documentoPath.path) {
DispatchQueue.main.async {
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [documentoPath], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.downloadNavController?.present(activityViewController, animated: true, completion: { () in
UIBarButtonItem.appearance().tintColor = UIColor.systemBlue
UINavigationBar.appearance().barTintColor = UIColor.white
})
}
} else {
print("document was not found")
}
Upvotes: 1
Reputation: 91
Check my simple code might be helpful to you and don't forget to configure share setting for whatsapp after only it will appear in sharing instance..
@objc func shareTapped() {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: documentPath!))
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [docTitle!,data], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
print("pdf file loading...")
}
catch {
let msgView = Utils.swiftMeassageView(title: "", message: "PDF file not found.", theme: .info)
SwiftMessages.show(view: msgView)
}
}
Upvotes: 1