ResearchKit activities controller managing a survey.
// | |
// ActivitiesViewController.swift | |
// MyStudyApp | |
// | |
// Created by Vincent Tourraine on 2/8/16. | |
// Copyright © 2016 Shazino. All rights reserved. | |
// | |
import UIKit | |
import ResearchKit | |
class ActivitiesViewController: UIViewController, ORKTaskViewControllerDelegate { | |
let QuestionStepIdentifier = "yes-no-step" | |
@IBAction func startSurvey(sender: UIButton) { | |
let introStep = ORKInstructionStep(identifier: ORKInstruction0StepIdentifier) | |
introStep.title = NSLocalizedString("Basic Survey", comment: "") | |
introStep.text = NSLocalizedString("This is a basic survey.", comment: "") | |
let step = ORKQuestionStep(identifier: QuestionStepIdentifier) | |
step.title = NSLocalizedString("Do you feel good?", comment: "") | |
step.answerFormat = ORKBooleanAnswerFormat() | |
let completionStep = ORKOrderedTask.makeCompletionStep() | |
let task = ORKOrderedTask(identifier: "yes-no-task", steps:[introStep, step, completionStep]) | |
let viewController = ORKTaskViewController(task: task, taskRunUUID: nil) | |
viewController.delegate = self | |
presentViewController(viewController, 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)") | |
if reason == .Completed { | |
if let stepResult = taskViewController.result.stepResultForStepIdentifier(QuestionStepIdentifier), | |
let stepResults = stepResult.results, | |
let stepFirstResult = stepResults.first, | |
let booleanResult = stepFirstResult as? ORKBooleanQuestionResult, | |
let booleanAnswer = booleanResult.booleanAnswer { | |
print("Result for question: \(booleanAnswer.boolValue)") | |
} | |
} | |
let durationFormatter = NSNumberFormatter() | |
durationFormatter.maximumFractionDigits = 2 | |
if let results = taskViewController.result.results { | |
for result in results { | |
guard let startDate = result.startDate, | |
let endDate = result.endDate | |
else { continue } | |
let duration = endDate.timeIntervalSinceDate(startDate) | |
let formattedDuration = durationFormatter.stringFromNumber(duration) | |
print("Duration for step “\(result.identifier)”: \(formattedDuration!) s") | |
} | |
} | |
dismissViewControllerAnimated(true, completion: nil) | |
} | |
} | |
This comment has been minimized.
This comment has been minimized.
I doesn't work on xcode 8.2.1. Could you gently put an xcode project to test it? Thanks. |
This comment has been minimized.
This comment has been minimized.
I got most of this working in Xcode 12 beta 4 with some reasonable tweaks. Slight warning to other users: in line 18 the ORKInstruction0StepIdentifier will build and function in simulation, but will fail when attempting to archive. Seems to be fixed by replacing it with any string identifier. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Output example: