Skip to content

Instantly share code, notes, and snippets.

@AD-Paladins
Last active November 16, 2022 08:24
Show Gist options
  • Save AD-Paladins/6a273b6c8880140804b655a4e23187c7 to your computer and use it in GitHub Desktop.
Save AD-Paladins/6a273b6c8880140804b655a4e23187c7 to your computer and use it in GitHub Desktop.
Store base64 String to file and share it
import UIKit
class ViewController: UIViewController {
@IBAction func sharePDFActionButton(_ sender: Any) {
do {
try savePdf()
loadPDFAndShare()
} catch {
print("FALLO EL GUARDAR EL PDF")
}
}
let pdfString = "Put your base64 encoded PDF here"
override func viewDidLoad() {
super.viewDidLoad()
}
func savePdf() throws {
let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let pdfDocURL = documentsURL.appendingPathComponent("document.pdf")
let pdfData = Data(base64Encoded: pdfString)
try pdfData!.write(to: pdfDocURL)
}
func loadPDFAndShare(){
do {
let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let pdfDocURL = documentsURL.appendingPathComponent("document.pdf")
let document = NSData(contentsOf: pdfDocURL)
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [document!], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView=self.view
present(activityViewController, animated: true, completion: nil)
print("document was not found")
} catch {
print("document was not found")
}
}
}
@givinalis
Copy link

This is actually a brilliant solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment