Search code examples
iosswiftpdfpdf-generationresearchkit

Swift ResearchKit makePDFWithCompletionHandler


I'm currently working on a medical research app in swift, based on the ResearchKit framework (which is written in objective-c). I have the signature assigned to the consent document and I'm trying to create a PDF using makePDFWithCompletionHandler and email it. This is the completion handler I currently have for the consent task in my view controller:

func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
    taskViewController.dismissViewControllerAnimated(true, completion: nil)
    if reason == .Completed {
            ConsentDocument.makePDFWithCompletionHandler(/*Completion handler Block*/){
                //Email pdf code here
            }
        }
}

I cannot figure out what to put as the completion handler block. Also, I can't find code to email the pdf once it is created.

In my consent task, I have the following code to assign the signature to the document:

let signatureResult = ORKConsentSignatureResult(identifier: "ConsentDocumentParticipantSignature")
signatureResult.applyToDocument(ConsentDocument)

Solution

  • For the makePDFWithCompletionHandler completion block, this works for me (note, this writes it to a file in the block):

       ConsentDocument .makePDFWithCompletionHandler({ (NSData pdfFile, NSError error) -> Void in
                // println("pdf created")
    
                // finding document path  //TODO: Remove if not needed
    
                let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] .stringByAppendingPathComponent("Consent.pdf")
    
                pdfFile!.writeToFile(documentsPath, atomically: false)
    
                println(consentDocumentFromDirectory)
    
    
    
    
            })