Search code examples
iosswiftresearchkit

Implement ResearchKit in a ViewController to make a survey


EDIT: Not sure if this may be the root of my issue, but would this piece of code in the app delegate be a reason why this is not working?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let navigationController = self.window!.rootViewController as! UINavigationController
    let controller = navigationController.topViewController as! HomeViewController
    controller.managedObjectContext = self.managedObjectContext
    return true

}

I am trying to add survey functionality to an app with integration of ResearchKit. I have worked through the setup guide and some other tutorials from Ray Wenderlich. However when I transitioned into an app I would like to develop I got a bit stuck.

I am getting thrown an error: Cannot assign value of type 'HomeviewController to type 'ORKTaskViewControllerDelegate?'

This is the code I am working with:

class HomeViewController: UIViewController {
var managedObjectContext: NSManagedObjectContext?

@IBAction func surveyTapped(sender: AnyObject) {

    let taskViewController = ORKTaskViewController(task: SurveyTask, taskRunUUID: nil)
    taskViewController.delegate = self
    presentViewController(taskViewController, animated: true, completion: nil)

}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.destinationViewController.isKindOfClass(NewRideViewController) {
        if let newRideViewController = segue.destinationViewController as? NewRideViewController {
            newRideViewController.managedObjectContext = managedObjectContext
        }
    }
}
}

Based on similar questions with this error syntax, users have added another controller class, but which one is beyond me. Your help is greatly appreciated, thank you StackExchange!


Solution

  • Looks like you have not extended you view controller to implement delegate of ORKTaskViewController which is ORKTaskViewControllerDelegate and your VC code should be as follow -

    import ResearchKit
    
    class HomeVC: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        @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?) {
    
        }
    
    }