Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vtourraine
Created March 23, 2016 12:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vtourraine/b090541bd3348a40d37c to your computer and use it in GitHub Desktop.
Save vtourraine/b090541bd3348a40d37c to your computer and use it in GitHub Desktop.
ResearchKit onboarding controller managing consent.
//
// OnboardingViewController.swift
// MyStudyApp
//
// Created by Vincent Tourraine on 2/8/16.
// Copyright © 2016 Shazino. All rights reserved.
//
import UIKit
import ResearchKit
class OnboardingViewController: UIViewController, ORKTaskViewControllerDelegate {
let ConsentReviewStepIdentifier = "ConsentReviewStep"
let consentDocument = ORKConsentDocument()
@IBAction func joinButtonTapped(sender: UIButton) {
consentDocument.title = NSLocalizedString("Research Health Study Consent Form", comment: "")
let section1 = ORKConsentSection(type: .Overview)
section1.summary = NSLocalizedString("Section 1 Summary", comment: "")
section1.content = NSLocalizedString("Section 1 Content...", comment: "")
let section2 = ORKConsentSection(type: .DataGathering)
section2.summary = NSLocalizedString("Section 2 Summary", comment: "")
section2.content = NSLocalizedString("Section 2 Content...", comment: "")
let section3 = ORKConsentSection(type: .Privacy)
section3.summary = NSLocalizedString("Section 3 Summary", comment: "")
section3.content = NSLocalizedString("Section 3 Content...", comment: "")
consentDocument.sections = [section1, section2, section3]
let signature = ORKConsentSignature(forPersonWithTitle: "Participant", dateFormatString: nil, identifier: "ConsentDocumentParticipantSignature")
consentDocument.addSignature(signature)
let consentStep = ORKVisualConsentStep(identifier: "VisualConsentStep", document: consentDocument)
let reviewConsentStep = ORKConsentReviewStep(identifier: ConsentReviewStepIdentifier, signature: signature, inDocument: consentDocument)
let passcodeStep = ORKPasscodeStep(identifier: "Passcode")
passcodeStep.text = "Now you will create a passcode to identify yourself to the app and protect access to information you've entered."
let completionStep = ORKCompletionStep(identifier: "CompletionStep")
completionStep.title = NSLocalizedString("Welcome aboard.", comment: "")
completionStep.text = NSLocalizedString("Thank you for joining this study.", comment: "")
let orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, /*healthDataStep,*/ passcodeStep, completionStep])
let taskViewController = ORKTaskViewController(task: orderedTask, taskRunUUID: nil)
taskViewController.delegate = self
presentViewController(taskViewController, animated: true, completion: nil)
}
// MARK: Task view controller delegate
func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
print("Task run UUID: \(taskViewController.taskRunUUID.UUIDString)")
switch reason {
case .Completed:
let result = taskViewController.result
if let stepResult = result.stepResultForStepIdentifier(ConsentReviewStepIdentifier),
let signatureResult = stepResult.results?.first as? ORKConsentSignatureResult {
signatureResult.applyToDocument(consentDocument)
consentDocument.makePDFWithCompletionHandler { (data, error) -> Void in
let tempPath = NSTemporaryDirectory() as NSString
let path = tempPath.stringByAppendingPathComponent("signature.pdf")
data?.writeToFile(path, atomically: true)
print(path)
}
}
default:
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment