Skip to content

Instantly share code, notes, and snippets.

@bishalg
Created March 24, 2016 07:30
Show Gist options
  • Save bishalg/c5268460b81344451e44 to your computer and use it in GitHub Desktop.
Save bishalg/c5268460b81344451e44 to your computer and use it in GitHub Desktop.
ResearchKit - example of ORKOrderedTask for ORKTaskViewController with ORKCompletionStep
//
// HomeVC.swift
// LoginKit
//
// Created by Bishal Ghimire on 2/24/16.
// Copyright © 2016 Bishal Ghimire. All rights reserved.
//
import UIKit
import ResearchKit
class HomeVC: UIViewController {
@IBOutlet weak var gettingStartedButton: UIButton!
@IBAction func gettingStartedAction(sender: UIButton) {
self.sayHellowToTheWorld()
}
func sayHellowToTheWorld() {
// Step - 1
let stepStart = ORKInstructionStep(identifier: "step1")
stepStart.title = "Hello World!"
let stepQuestion1 = ORKQuestionStep(identifier: "QuestionNo1")
stepQuestion1.title = "Are you an iOS developer ?"
let yesNoAnswerFormat = ORKAnswerFormat.booleanAnswerFormat()
stepQuestion1.answerFormat = yesNoAnswerFormat
let stepEnd = ORKCompletionStep(identifier: "step2")
stepEnd.title = "That is it ! :] Thanks"
let task = ORKOrderedTask(identifier: "ourFirstTask", steps: [stepStart, stepQuestion1, stepEnd])
let taskVC = ORKTaskViewController(task: task, taskRunUUID: nil)
taskVC.delegate = self
presentViewController(taskVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
}
@IBAction func surveyTapped(sender: AnyObject) {
let taskViewController = ORKTaskViewController(task: SurveyTask, taskRunUUID: nil)
taskViewController.delegate = self
presentViewController(taskViewController, animated: true, completion: nil)
}
}
extension HomeVC: ORKTaskViewControllerDelegate {
func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
self.dismissViewControllerAnimated(true, completion: nil)
let buttonTitle: String?
switch reason {
case .Completed:
buttonTitle = "Completed"
case .Discarded:
buttonTitle = "Discarded"
case .Failed:
buttonTitle = "Failed"
case .Saved:
buttonTitle = "Saved"
}
gettingStartedButton.setTitle(buttonTitle, forState: .Normal)
let allResults = taskViewController.result.results
for stepResults in allResults! as! [ORKStepResult] {
print("---")
for result in stepResults.results! {
// ORKBooleanQuestionResult
if result.identifier == "QuestionNo1" {
print("found question No 1")
}
print(result)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment