Skip to content

Instantly share code, notes, and snippets.

@Parietal
Forked from chrishinds/AppDelegate.swift
Last active July 5, 2018 03:42
Show Gist options
  • Save Parietal/66905a300862f8987774 to your computer and use it in GitHub Desktop.
Save Parietal/66905a300862f8987774 to your computer and use it in GitHub Desktop.
Example in Swift of subclassing an ORKActiveStepViewController in Apple ResearchKit, then laying out a custom view
import UIKit
import ResearchKit
class DemoView: UIView {
}
class DemoStepViewController : ORKActiveStepViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red:0.0, green:1.0, blue:0.0, alpha:1.0)
let demoView = DemoView()
demoView.setTranslatesAutoresizingMaskIntoConstraints(false)
demoView.backgroundColor = UIColor(red:1.0, green:0.0, blue:0.0, alpha:1.0)
self.customView = demoView
self.customView?.superview!.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[demoView]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["demoView": demoView]))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[demoView]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["demoView": demoView]))
}
}
class DemoStep : ORKActiveStep {
static func stepViewControllerClass() -> DemoStepViewController.Type {
return DemoStepViewController.self
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let activeStep = DemoStep(identifier: "ds_id")
activeStep.title = "Demo Step"
var endStep = ORKCompletionStep(identifier: "es_id")
endStep.title = "Well done"
endStep.text = "thank you"
let task = ORKOrderedTask(identifier: "ts_id", steps: [activeStep,endStep])
let taskViewController = ORKTaskViewController(task: task, taskRunUUID: nil)
taskViewController.delegate = self
taskViewController.outputDirectory = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String, isDirectory: true)
window?.rootViewController = taskViewController
return true
}
}
extension AppDelegate : ORKTaskViewControllerDelegate {
func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
//Handle results with taskViewController.result
taskViewController.dismissViewControllerAnimated(true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment