I downloaded the latest release of ResearchKit from GitHub and embedded the framework in my project.
Using the Ray Wenderlich Tutorial on ResearchKit, I created a Survey Task:
import ResearchKit
public var SurveyTask: ORKOrderedTask {
var steps = [ORKStep]()
let instructionStep = ORKInstructionStep(identifier: "IntroStep")
instructionStep.title = "The Questions Three"
instructionStep.text = "Who would cross the Bridge of Death must answer me these questions three, ere the other side they see."
steps += [instructionStep]
let nameAnswerFormat = ORKTextAnswerFormat(maximumLength: 20)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle = "What is your name?"
let nameQuestionStep = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle, question: "Hello?", answer: nameAnswerFormat)
steps += [nameQuestionStep]
let questQuestionStepTitle = "What is your quest?"
let textChoices = [
ORKTextChoice(text: "Create a ResearchKit App", value: 0 as NSNumber),
ORKTextChoice(text: "Seek the Holy Grail", value: 1 as NSNumber),
ORKTextChoice(text: "Find a shrubbery", value: 2 as NSNumber)
]
let questAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: textChoices)
let questQuestionStep = ORKQuestionStep(identifier: "TextChoiceQuestionStep", title: questQuestionStepTitle, question: "Hello?", answer: questAnswerFormat)
steps += [questQuestionStep]
let summaryStep = ORKCompletionStep(identifier: "SummaryStep")
summaryStep.title = "Right. Off you go!"
summaryStep.text = "That was easy!"
steps += [summaryStep]
return ORKOrderedTask(identifier: "SurveyTask", steps: steps)
}
the delegate in my ViewController is set up like so:
extension ViewController: ORKTaskViewControllerDelegate {
func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
taskViewController.dismiss(animated: true, completion: nil)
}
}
and then in that ViewController, I tried to present the ORKTaskViewController
:
@objc func showSurvey() {
let taskViewController = ORKTaskViewController(task: SurveyTask, taskRun: nil)
taskViewController.delegate = self
self.present(taskViewController, animated: true, completion: nil)
}
I keep getting libc++abi.dylib: terminating with uncaught exception of type NSException
, and I'm not quite sure why. I'm not using storyboards, but I don't see why I wouldn't be able to use ResearchKit without storyboard. I've tried to search for storyboard-less implementations of this, but the closest I could find is this which still uses storyboard. Any help would be appreciated.
After adding an exception breakpoint, I found that it had something to do with _appTintColor
in the ResearchKit library.
This line of code in ORKNavigationContainerView.m
:
_appTintColor = [[UIApplication sharedApplication].delegate window].tintColor;
did not work and I'm assuming this is because SceneDelegate.swift
was introduced in iOS 13, which is where I'm programmatically setting the window and root view controller (as opposed to doing it in AppDelegate).
If you change it to something like this:
if (@available(iOS 13.0, *)) {
_appTintColor = [self window] .tintColor;
// not sure if it's this or [[self window].windowScene windows].firstObject .tintColor;
} else {
_appTintColor = [[UIApplication sharedApplication].delegate window].tintColor;
}
then it should work. I'm not familiar with Obj-c but I was trying to get the window of the view that instantiates the new view controller, and it seems to work (feel free to edit).