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)

通过获得审查步骤的签名并将其应用于同意书,您可以使用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
}

对于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)


        })

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

相关内容

  • 没有找到相关文章

最新更新