You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

9.0 KiB

RK-Setup-Git

Introduction

This repository explains how to setup ResearchKit V3 using Git Submodules. The repository has two branches: example and main. The example branch shows a basic sourvey task example using the added ResearchKit framework. Then, the main branch is a project reaady to start working with RK V3.

Example branch

This section explains how to create a project to work with ResearchKit v3 using Git submodules. The main idea is to use this repository as a starting point, however, if you want to create your own Xcode project from zero, follow the next guide

Step 1: Setting up the Xcode project

To set up ResearchKit v3 using Git submodules, you can include the ResearchKit repository as a submodule in your Xcode project instead of using CocoaPods.

  1. Open Terminal and navigate to your Xcode projects directory:
cd /path/to/your/project
  1. Add the ResearchKit repository as a submodule:
git submodule add https\://github.com/ResearchKit/ResearchKit.git
  1. Initialize and update the submodule:
git submodule update --init --recursive

This command will clone the ResearchKit repository into a ResearchKit folder inside your project directory.

Thus, your project folder (in my case RK-Setup-Git should looks like the next image:

project

Step 2: Add ResearchKit to Your Xcode Project

  1. Open your Xcode project or workspace.
  2. In the Xcode Project Navigator, right-click on the projects root and select Add Files to “YourProjectName”.

adding RK 3. Browse to the ResearchKit folder you cloned as a submodule and select the ResearchKit.xcodeproj file.

adding the xcodeproj file

  1. Add ResearchKit as a dependency:
  • Select your Project in the Project Navigator.
  • Go to the General tab of your target.
  • Under Frameworks, Libraries, and Embedded Content, click the + button. CleanShot 2024-10-11 at 17.17.38@2x
  • Select ResearchKit.framework (and other frameworks that you requiere) from the list (you should see it under “Workspace”). CleanShot 2024-10-11 at 17.18.46@2x
  1. Set ResearchKit to Embed and Sign:
  • Under Frameworks, Libraries, and Embedded Content, ensure that the ResearchKit.framework is set to Embed & Sign. CleanShot 2024-10-11 at 17.24.12@2x

Step 3: Configure Build Settings**

  1. Go to the Build Phases tab for your app target.

  2. Link ResearchKit:

  • In the Link Binary with Libraries section, ensure that ResearchKit.framework is included.

  • If it is not listed, click the + button, and select ResearchKit.framework.

CleanShot 2024-10-11 at 17.28.09@2x 3. Add ResearchKit as a Target Dependency:

  • In the Target Dependencies section, click the + button and add ResearchKit. CleanShot 2024-10-11 at 17.29.21@2x

Step 4: Use ResearchKit in Your Project

Now that ResearchKit is properly set up as a submodule, you can start using it in your code:

  1. Import ResearchKit in the Swift files where you want to use it:
import ResearchKit

if you can write import Resea and Xcode shows the auto comple suggestion to include ResearchKit, your project is ready.

CleanShot 2024-10-11 at 17.33.22@2x

  1. Create and present ResearchKit tasks as needed.

Summary

By using ResearchKit as a Git submodule, you gain more control over the version youre using, and you keep dependencies managed directly through Git. This approach is especially useful when you want to track changes in ResearchKit or if you prefer not to use CocoaPods for dependency management.

Example of usage with a survey task

For the implementation of this example we are going to add a new swift file, as shown in the following image and named it SurveyTask:

CleanShot 2024-10-11 at 17.38.33@2x

The content of the SurveyTask.swift file[1^]:

import ResearchKit

struct SurveyTaskProvider {
    
    static func createHeightFormTask() -> ORKOrderedTask {
        
        let sectionHeaderFormItem = ORKFormItem(sectionTitle: "What's your height?")
        let heightQuestionFormItem = ORKFormItem(identifier: "heightQuestionFormItem1", text: nil, answerFormat: ORKAnswerFormat.heightAnswerFormat())
        
        heightQuestionFormItem.placeholder = "Tap here"
        
        let formStep = ORKFormStep(identifier: "HeighQuestionIdentifier", 
                                   title: "Height info",
                                   text: "Please answer the questions")
        
        formStep.formItems = [sectionHeaderFormItem, heightQuestionFormItem]
        
        return ORKOrderedTask(identifier: "Survey Task", steps: [formStep])
    }
}

The ViewController file should be modified to make and Outlet and Action for a button to deploy the Survey:

import UIKit
import ResearchKit
import ResearchKitUI


class ViewController: UIViewController {
    
    @IBOutlet weak var startTapped: UIButton! // variable for the button
    
    @IBAction func startSurveyTapped(_ sender: Any) { // action for button
        presentHeightSurvey()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        startTapped.setTitle("Start Survey", for: .normal)
    }
    
    func presentHeightSurvey() {
        let taskViewController = ORKTaskViewController(task: SurveyTaskProvider.createHeightFormTask(), taskRun: nil)
        taskViewController.delegate = self
        present(taskViewController, animated: true, completion: nil)
    }
}

// Extesions can be in other file:

extension ViewController: ORKTaskViewControllerDelegate {
    func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskFinishReason, error: (any Error)?) {
        
        // Handle the completion of the task and dismiss the view controller
        taskViewController.dismiss(animated: true, completion: nil)
        
        //handle error
        guard reason == .completed else {
            if let error = error {
                print("The task was not completed: \(error.localizedDescription)")
            }
            return
        }
        
        // processing results:
        if let results = taskViewController.result.results as? [ORKStepResult] {
            processSurveyResults(results)
        }
    }
    
    private func processSurveyResults(_ results: [ORKStepResult]) {
        // here
        for stepResults in results {
            for result in stepResults.results! {
                if let questionResult = result as? ORKQuestionResult {
                    print("Question ID: \(questionResult.identifier)")
                    processQuestionResult(questionResult)
                }
            }
        }
    }
    
    
    private func processQuestionResult(_ result: ORKQuestionResult) {
        if let heightResult = result as? ORKNumericQuestionResult, result.identifier == "heightQuestionFormItem1" {
            if let heigth = heightResult.numericAnswer {
                print("User's heigth is: \(heigth) cm")
            }
        }
    }
    
}
  • Finally add the button component: CleanShot 2024-10-11 at 17.43.50@2x
  • drag and drop the button element:

CleanShot 2024-10-11 at 17.44.41@2x

  • place the button in the area that you prefer: CleanShot 2024-10-11 at 17.45.04@2x then, you must associate the already writted @Outlet and @Action by placing and extra editor at right pressing the + button as shown next:

CleanShot 2024-10-11 at 18.13.04@2x

  • Next, place at the left the Main story board (View controller scene) and at right the ViewCotroller code to associete the elements:

CleanShot 2024-10-11 at 18.13.23@2x Press CTR key and drag and drop the button element at the @Outlet and @Action code lines as shown next:

CleanShot 2024-10-11 at 17.52.58

This means that the button component is now asociated with the startTapped var name and with the action when button is pressed. Remember that you can create your own outlets and actions by dragging in any line at the viewcontroller file.

[1^]: GitHub - ResearchKit/ResearchKit: ResearchKit is an open source software framework that makes it easy to create apps for medical research or for other research projects.