Swift ResearchKit makePDFWithCompletionHandler

Swift ResearchKit makePDFWithCompletionHandler

我目前正在 swift 开发基于 ResearchKit 框架(用 objective-c 编写)的医学研究应用程序。我已将签名分配给同意文档,我正在尝试使用 makePDFWithCompletionHandler 创建 PDF 并将其通过电子邮件发送。这是我目前在我的视图控制器中用于同意任务的完成处理程序:

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

我不知道应该把什么作为完成处理程序块。另外,我找不到用于在创建 pdf 后通过电子邮件发送它的代码。

在我的同意任务中,我有以下代码将签名分配给文档:

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

对于 makePDFWithCompletionHandler 完成块,这对我有用(注意,这会将其写入块中的文件):

   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)




        })

通过获取审核步骤的签名并将其应用于同意文档,您可以使用 makePdf 完成块制作 pdf 并保存在磁盘上或发送到服务器。

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

}

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

                    }

                }