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.

223 lines
9.0 KiB
Markdown

2 months ago
# 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:
```sh
cd /path/to/your/project
```
2. Add the ResearchKit repository as a submodule:
```sh
git submodule add https\://github.com/ResearchKit/ResearchKit.git
```
3. Initialize and update the submodule:
```sh
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](DA2y0wyAI-CleanShot_2024-10-11_at_16.55.13@2x.jpg)
### 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](w3ujSOtx2-CleanShot_2024-10-11_at_17.00.34@2x.jpg)
3. Browse to the ResearchKit folder you cloned as a submodule and select the ResearchKit.xcodeproj file.
![adding the xcodeproj file](uLAeiu5Fl-CleanShot_2024-10-11_at_17.02.36@2x.jpg)
4. 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](w-4AupjJR-CleanShot_2024-10-11_at_17.17.38@2x.jpg)
* 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](9TyP3BaAn-CleanShot_2024-10-11_at_17.18.46@2x.jpg)
5. **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](hGqSnwva6-CleanShot_2024-10-11_at_17.24.12@2x.jpg)
### 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](ICrsjPjwp-CleanShot_2024-10-11_at_17.28.09@2x.jpg)
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](E7AA2yFVA-CleanShot_2024-10-11_at_17.29.21@2x.jpg)
**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:
```swift
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](RFtEguouj-CleanShot_2024-10-11_at_17.33.22@2x.jpg)
2. 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](RZLt5GIeJ-CleanShot_2024-10-11_at_17.38.33@2x.jpg)
The content of the `SurveyTask.swift` file[1^]:
```swift
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:
```swift
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](0UFezqS5q-CleanShot_2024-10-11_at_17.43.50@2x.jpg)
* drag and drop the button element:
![CleanShot 2024-10-11 at 17.44.41@2x](BYbiPcQIc-CleanShot_2024-10-11_at_17.44.41@2x.jpg)
* place the button in the area that you prefer:
![CleanShot 2024-10-11 at 17.45.04@2x](-LVIKgGeZ-CleanShot_2024-10-11_at_17.45.04@2x.jpg)
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](GWe48wvO2-CleanShot_2024-10-11_at_18.13.04@2x.jpg)
* 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](5X1xHgjLf-CleanShot_2024-10-11_at_18.13.23@2x.jpg)
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](cOVGAyV8--CleanShot_2024-10-11_at_17.52.58.gif)
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.](https://github.com/ResearchKit/ResearchKit)