Gizmodo
Gizmodo

Reputation: 3222

Sharing PDF with UIActivityViewController - File Name

I am creating a PDF and sharing it via UIActivityViewController.

func createFlyer() -> Data {
  // 1
  let pdfMetaData = [
    kCGPDFContextCreator: "Flyer",
  ]
  let format = UIGraphicsPDFRendererFormat() 
  format.documentInfo = pdfMetaData as [String: Any]

  // 2
  let pageWidth = 8.5 * 72.0
  let pageHeight = 11 * 72.0
  let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)

  // 3
  let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)
  // 4
  let data = renderer.pdfData { (context) in
    // 5
    context.beginPage()
    // 6
    let attributes = [
      NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 72)
    ]
    let text = "I'm a PDF!"
    text.draw(at: CGPoint(x: 0, y: 0), withAttributes: attributes)
  }

  return data
}

func sharePDF () {
    let activityViewController = UIActivityViewController(
        activityItems: [data],  applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivity.ActivityType.saveToCameraRoll]

    if let popoverPresentationController = activityViewController.popoverPresentationController {
        popoverPresentationController.barButtonItem = self.sharePDFButton
    }
    self.present(activityViewController, animated: true, completion: {
    })   
}

Everything is fine, except for the file name is generated by the system ('PDF Document.pdf').

Is there a way to have a custom file name?

Upvotes: 0

Views: 1344

Answers (1)

Varsha Shivhare
Varsha Shivhare

Reputation: 91

Please try to use below code while presenting activityViewController, I hope it works.

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
          })
       }
 }

You can find getDirectoryPath() here:

func getDirectoryPath() -> URL {
    var nestedFolderURL: URL?
    do{
        let rootFolderURL = try FileManager.default.url(
            for: .documentDirectory,
            in: .userDomainMask,
            appropriateFor: nil,
            create: false
        )
        nestedFolderURL = rootFolderURL
        
        if !FileManager.default.fileExists(atPath: nestedFolderURL!.relativePath) {
            try FileManager.default.createDirectory(
                at: nestedFolderURL!,
                withIntermediateDirectories: false,
                attributes: nil
            )
        }
        return nestedFolderURL!
    } catch (let error) {
        print(error)
    }
    
    return nestedFolderURL!
}

Upvotes: 2

Related Questions