1

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)
Ayaan
  • 57
  • 7

3 Answers3

3

By getting the signature for review step and applying that on consent document, you can make pdf with makePdf completion block and can save on your disk or can send to server.

func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
    //Handle results with taskViewController.result
    let taskResult = taskViewController.result

    if reason  == ORKTaskViewControllerFinishReason.Completed
    {
        var signatureResult : ORKConsentSignatureResult = taskResult.stepResultForStepIdentifier("ConsentReviewStep")?.firstResult as! ORKConsentSignatureResult
        let document = ConsentDocument.copy() as! ORKConsentDocument
        signatureResult.applyToDocument(document)
        document.makePDFWithCompletionHandler({ (pdfData:NSData?, error: NSError?) -> Void in

            var docURL = (NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)).last as? NSURL                
            docURL = docURL?.URLByAppendingPathComponent( "myFileName.pdf")

            //write your file to the disk.
            pdfData?.writeToURL(docURL!, atomically: true)
            //now you can see that pdf in your applications directory

}
Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63
Nakul Sharma
  • 600
  • 1
  • 16
  • 23
1

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)




        })
0

Swift 3.1

let result = taskViewController.result
                if let stepResult = result.stepResult(forStepIdentifier: ConsentReviewStepIdentifier),
                    let signatureResult = stepResult.results?.first as? ORKConsentSignatureResult {
                    signatureResult.apply(to: consentDocument)

                    consentDocument.makePDF { (data, error) -> Void in
                        var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last as NSURL?
                        docURL = docURL?.appendingPathComponent("myFileName.pdf") as NSURL?


                        //write your file to the disk.


                        do {

                            try data?.write(to:docURL! as URL)
                            print(docURL! as URL)




                        } catch let error {

                            print(error.localizedDescription)
                        }


                        //now you can see that pdf in your applications directory

                    }

                }
pawisoon
  • 1,427
  • 1
  • 15
  • 20