0

Hello I have simple app im trying to create html to pdf into webview and looking success but i want to add button share with UIActivityViewController ( Into Print,Mail etc.. )

My codes here.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    createPDF();
    loadPDF("file.pdf");

    // ActivityViewController

    btn!.setTitle("Open Menu", forState: .Normal)
    btn!.addTarget(self, action: "pressBtn:", forControlEvents: .TouchUpInside)
    btn!.sizeToFit()
    btn!.center = view.center
    view.addSubview(btn!)

}


func createPDF() {
    let html = "<b>Hello <i>World!</i></b> <p>Generate PDF file from HTML in Swift</p>"
    let fmt = UIMarkupTextPrintFormatter(markupText: html)

    // 2. Assign print formatter to UIPrintPageRenderer

    let render = UIPrintPageRenderer()
    render.addPrintFormatter(fmt, startingAtPageAtIndex: 0)

    // 3. Assign paperRect and printableRect

    let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
    let printable = CGRectInset(page, 0, 0)

    render.setValue(NSValue(CGRect: page), forKey: "paperRect")
    render.setValue(NSValue(CGRect: printable), forKey: "printableRect")

    // 4. Create PDF context and draw

    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil)

    for i in 1...render.numberOfPages() {

        UIGraphicsBeginPDFPage();
        let bounds = UIGraphicsGetPDFContextBounds()
        render.drawPageAtIndex(i - 1, inRect: bounds)
    }

    UIGraphicsEndPDFContext();

    // 5. Save PDF file

    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]

    pdfData.writeToFile("\(documentsPath)/file.pdf", atomically: true)
}


func loadPDF(filename: String) {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    let filePath = "\(documentsPath)/file.pdf"
    let url = NSURL(fileURLWithPath: filePath)
    let urlRequest = NSURLRequest(URL: url)
    webView!.loadRequest(urlRequest)
}


func pressBtn(sender:UIButton){



    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    let filePath = "\(documentsPath)/file.pdf"
    let activityItems = [filePath]

    let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
    presentViewController(activityViewController, animated: true, completion: nil)

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

And when click button gives this error

 *** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x79b601b0>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
*** First throw call stack:

Thanks for help.

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
  • Are you receiving this error only on iPad? This should help: http://stackoverflow.com/questions/25644054/uiactivityviewcontroller-crashing-on-ios8-ipads – Tish Jan 20 '16 at 17:02

2 Answers2

1

Have you tried to specify the source view before calling the presentViewController?

let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = sender
presentViewController(activityViewController, animated: true, completion: nil)
diegomen
  • 1,804
  • 1
  • 23
  • 37
1

The error might be something else,

Any ways, you would want to use UIDocumentInteractionController not UIActivityController

Set uti to "com.adobe.pdf"

Omar Al-Shammary
  • 304
  • 3
  • 13