Created
March 23, 2016 12:51
-
-
Save vtourraine/b090541bd3348a40d37c to your computer and use it in GitHub Desktop.
ResearchKit onboarding controller managing consent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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