iosswiftiphonexcoderesearchkit

Requiring consent before proceeding with survey in ResearchKit


I'm currently creating a program that wants to utilize ResearchKit. Before getting into survey questions, I will need to require consent. I am using the Ray Wenderlich (https://www.raywenderlich.com/1820-researchkit-tutorial-with-swift-getting-started) tutorial and have this code set up. I've already tried to go on with the survey part of my program, and I can access it without even going through consent.

import ResearchKit

public var ConsentTask: ORKOrderedTask {

    var ctr = 0

    let Document = ORKConsentDocument()
    Document.title = "Consent"

    //section types
    let sectionTypes: [ORKConsentSectionType] = [
        .overview,
        .dataGathering,
        .privacy
    ]

    let consentSections: [ORKConsentSection] = sectionTypes.map { contentSectionType in
        let consentSection = ORKConsentSection(type: contentSectionType)

        if ctr < sectionTypes.count {

            if ctr == 0 { 
                consentSection.summary = "Summary"
                consentSection.content = "Content"
            }

            else if ctr == 1 { //Data Gathering
                consentSection.summary = "Summary"
                consentSection.content = "Content"
            }

            else if ctr == 2 { //Privacy
                consentSection.summary = "Summary"
                consentSection.content = "Content"
            }

            ctr = ctr + 1
        }
        return consentSection
    }

    Document.sections = consentSections
    Document.addSignature(ORKConsentSignature(forPersonWithTitle: nil, dateFormatString: nil, identifier: "UserSignature"))

    var steps = [ORKStep]()

    let visualConsentStep = ORKVisualConsentStep(identifier: "VisualConsent", document: Document)
    steps += [visualConsentStep]

    let signature = Document.signatures!.first! as ORKConsentSignature
    let reviewConsentStep = ORKConsentReviewStep(identifier: "Review", signature: signature, in: Document)
    reviewConsentStep.text = "Review the consent"
    reviewConsentStep.reasonForConsent = "I agree"

    steps += [reviewConsentStep]

    //Completion
    let completionStep = ORKCompletionStep(identifier: "CompletionStep")
    completionStep.title = "Welcome"
    completionStep.text = "Thank you for helping!"
    steps += [completionStep]

    return ORKOrderedTask(identifier: "ConsentTask", steps: steps)
}

And in my view controller, I have

    @IBAction func consentTask(_ sender: Any) {

    if consentDone == false {
        let taskViewController = ORKTaskViewController(task: ConsentTask, taskRun: nil)
        taskViewController.delegate = self
        present(taskViewController, animated: true, completion: nil)
    }
    else if consentDone == true {
        //would they like to leave the study

    }
}

With my efforts of putting a consentDone flag here.

    func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {

    taskViewController.dismiss(animated: true, completion: {() in self.consentDone = true})
    print(consentDone)

}

However, what happens is that if the user presses cancel on the top right or done at the very end of the consent, it will trigger this always. Is there a way to be able to make sure that a block of code is executed only after everything is finished? Ideally, after this, I would like to make this a flag that the user has already finished the consent. I will redirect the user to a different page every time afterwards until the user leaves the study.


Solution

  • After some trial and error, I found the answer here: https://github.com/ResearchKit/ResearchKit/issues/919

    By knowing that the signature of the user means that the user has finished the form, we can do

      if result.identifier == "UserSignature"{
          print(result)
          let consentDoneAnswerResult = result as! ORKConsentSignatureResult
          let consentDone = consentDoneAnswerResult.consented
          print(consentDone)
      }
    

    And this gives consentDone as true when the form is done, not canceled.