I am working on a project using ResearchKit, so I send the user into the task I create:
let taskViewController = ORKTaskViewController(task: SurveyTask, taskRunUUID: nil)
taskViewController.delegate = self
presentViewController(taskViewController, animated: true, completion: nil)
When the user is finished with the survey, he goes into:
func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
switch reason {
case .Completed:
... }
Here I encounter a problem when I try to show an alert before
taskViewController.dismissViewControllerAnimated(true, completion: nil)
I get the below error:
Attempt to present UIAlertController: ... on ViewController: ... whose view is not in the window hierarchy
Any idea of how I could present the alert before dismissing the ViewController?
I use the below for the alert:
let alertView = UIAlertController(title: "Houps", message: "Could not connect to the server.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
EDIT: The code is as below:
if let httpResponse = response as? NSHTTPURLResponse {
print("HTTP response: \(httpResponse.statusCode)")
if httpResponse.statusCode == 201 {
taskViewController.dismissViewControllerAnimated(true, completion: nil)
}
} else {
print("No HTTP response")
let alertView = UIAlertController(title: "Houps", message: "Could not connect to the server.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
}
You should call presentViewController on the taskViewController:ORKTaskViewController object
taskViewController.presentViewController(alertView, animated: true, completion: nil)