Kodeco Forums

ResearchKit Tutorial with Swift: Getting Started

Learn how to make a medical research app with this ResearchKit tutorial for iOS.


This is a companion discussion topic for the original entry at http://www.raywenderlich.com/104575/researchkit-tutorial-with-swift

I added ResearchKit.framework as an Embedded Binary as shown in the tutorial, but when I checked by adding ā€œimport ResearchKitā€ to ViewController.swift, ResearchKit was not found.

This tutorial is very helpful. Is it possible to update it to swift2? Thanks.

You have to build ResearchKit First!

Also, the following changes have to be made for Swift 2.2 / Xcode 7.3

1. in AudioTask
public var MicrophoneTask: ORKOrderedTask {
return ORKOrderedTask.audioTaskWithIdentifier(ā€œAudioTaskā€, intendedUseDescription: ā€œA sentence prompt will be given to you to read.ā€, speechInstruction: ā€œThese are the last dying words of Joseph of Aramatheaā€, shortSpeechInstruction: ā€œSay this to Micā€, duration: 10, recordingSettings: nil, options: []) //options: nil
}

2. in Consent Task
//TODO: Add ConsentReviewStep
let signature = (consentDocument.signatures?.first)! as ORKConsentSignature //.signatures!.first as! ORKConsentSignature

3. Viewcontroller in Full Project Download
Corrected as

class ViewController: UIViewController {

}

Having problem with this on buildā€¦ Any ideas Error = Redundance conformance of ā€˜ViewControllerā€™ to protocol ā€˜ORKTaskViewControllerDelegateā€™

extension ViewController : ORKTaskViewControllerDelegate {

func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
    //Handle results with taskViewController.result
    taskViewController.dismissViewControllerAnimated(true, completion: nil)
}

}

Pl see my post above item 3.

If you have dowloaded full project source, it begins like

class ViewController: UIViewController, ORKTaskViewControllerDelegate {

}

Edit this to
class ViewController: UIViewController {

}

//create extension outside of
class ViewController: UIViewController {
as below

extension ViewController : ORKTaskViewControllerDelegate {

func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
    //Handle results with taskViewController.result
    taskViewController.dismissViewControllerAnimated(true, completion: nil)
}

}

1 Like

I use Xcode7.2. I also have problems with the codes of the view controller.
What Iā€™m doing wrong?

1 Like

Hi, i have the same problem as above^^^
Also, on the step that says: make UIButton, if anyone could help me doing this, that would be great! All help is welcome!

This tutorial is the best for newcomers! I have followed it and create my research kit app using xcode 7.3 at GitHub - zad/myresearchkitapp: My iOS ResearchKit app example followed by the tutorial (https://www.raywenderlich.com/104575/researchkit-tutorial-with-swift)

Thanks so much for that tutorial. I was wondering how to program the app so that the first time the user opens the app it requires them to go through the consent process. But that in the future, or when they open the app for second time and furthermore, they open with a dashboard ( i already created a dashboard).

Move the extension ViewController section below the last }. It needs to be outside the class definition.

Create the UIButton in the Main.storyboard

In this, it says ā€œThe textual content is set by the summary and content properties which provide the title and text for each section. In a real app, youā€™d definitely use different text on each page, according to its purpose. But for this tutorial, you can simply add the same summary and content to each sectionā€ How would you do this on each page? Iā€™m having a problem. Thanks!

Hi, I am quite new to xcode and researchkit and this tutorial has helped me a lot in my project. Can you please suggest a way for me to get the data that users input?

Iā€™m having trouble trying to create a custom ORKAudioStep with the ORKGraphView and some extra buttons to restart the recording. I want the default behavior just want to add in an extra button to restart recording.

if let stepResult = taskViewController.result.stepResult(forStepIdentifier: ā€œidentifierā€),
let stepResults = stepResult.results,
let stepFirstResult = stepResults.first,
let nameResult = stepFirstResult as? ORKFileResult,
let nameAnswer = nameResult.fileURL {
print(ā€œResult for name: (nameAnswer)ā€)
}

1 Like

Hello, can anyone please help me? Iā€™m trying to create a profile tab on my app. I tried looking at the ORKSample code, but tapping on the profile app causes an error saying: ā€œHealthdata not setā€.

Hello ,
I did tried this to create user Signature , but I am stuck.
Is this following code correct?
import ResearchKit

public var ConsentTask: ORKOrderedTask {

var steps = [ORKStep]()

let consentDocument =  ORKConsentDocument()
let visualConsentStep = ORKVisualConsentStep(identifier: "VisualConsentStep", document: consentDocument)
steps += [visualConsentStep]

let signature = consentDocument.signatures!.first!

let reviewConsentStep = ORKConsentReviewStep(identifier: "ConsentReviewStep", signature: signature, in: consentDocument)

reviewConsentStep.text = "Review Consent!"
reviewConsentStep.reasonForConsent = "Consent to join study"

steps += [reviewConsentStep]

return ORKOrderedTask(identifier: "ConsentTask", steps: steps)

}

//next swift file

public var ConsentDocument: ORKConsentDocument {

let consentDocument = ORKConsentDocument()
consentDocument.title = "Example Consent"

let consentSectionTypes: [ORKConsentSectionType] = [
    .Overview,
    .DataGathering,
    .Privacy,
    .DataUse,
    .TimeCommitment,
    .StudySurvey,
    .StudyTasks,
    .Withdrawing
]

var consentSections: [ORKConsentSection] = consentSectionTypes.map { contentSectionType in
    let consentSection = ORKConsentSection(type: contentSectionType)
    consentSection.summary = "End of this Month..."
    consentSection.content = "Please Sign."
    return consentSection
}

consentDocument.sections = consentSections

consentDocument.addSignature(ORKConsentSignature(forPersonWithTitle: nil, dateFormatString: nil, identifier: "ConsentDocumentParticipantSignature"))

return consentDocument

}

//UIViewcontroller
import UIKit
import ResearchKit

class SignatureVC: UIViewController {

@IBOutlet weak var imageView: UIImageView!



override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

@IBAction func click_Consent(_ sender: Any) {
    
    let taskViewController = ORKTaskViewController(task: ConsentTask, taskRun: nil)
    taskViewController.delegate = self
    present(taskViewController, animated: true, completion: nil)
}

}

extension patientSignatureVC : ORKTaskViewControllerDelegate {

func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
    //Handle results with taskViewController.result
    taskViewController.dismiss(animated: true, completion: nil)
}

}

Created on Viewcontroller in storyboard with one Button which is linked to SignatureVC.
Please help, where did I do mistake?

2017-08-28 12:04:00.871 Gamelot[5334:92546] *** Terminating app due to uncaught exception ā€˜NSGenericExceptionā€™, reason: ā€˜Visual consent step has no visible scenesā€™

this tutorial not working anymore on Swift 3

This tutorial is more than six months old, so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]

1 Like