Apple Health Frameworks

Nov 29 2022 · Swift 5, iOS 15, Xcode 13

Part 3: Take Advantage of CareKit & ResearchKit

11. Make an ActiveTask Using ResearchKit.

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Make a Follow-Up Vaccination Task Next episode: 12. Extract OCKOutcomeValue from ORKTaskResult

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 11. Make an ActiveTask Using ResearchKit.

Part 3, Episode 12, Make an ActiveTask using ResearchKit

In this episode, I want to show you how to make an active task to ask a user to do something physically by using the researchkit.

Let’s begin with TaskModel, open it from the project navigator, and then add a new case right after // Add MotionCheck Task like this:

case motionCheck

Then navigate to SurveyManager and at the botom of the class right after // Prepare Motion Survey - ResearchTask add new function:

  static func motionSurvey() -> ORKTask {
    let surveyTask = ORKOrderedTask.shoulderRangeOfMotionTask(
      withIdentifier: IdentifierModel.motionStep.rawValue,
      limbOption: .left,
      intendedUseDescription: nil,
      options: [.excludeConclusion]
    )
    let completionStep = ORKCompletionStep(identifier: IdentifierModel.motionCompletion.rawValue)
    completionStep.title = "All done!"
    completionStep.detailText = "We know the road to recovery can be painful. Keep up the good work!"
    surveyTask.appendSteps([completionStep])
    return surveyTask
  }

Here you made a new task called shoulderRangeOfMotionTask and set it for the left hand by default. Next is to open TaskManager then add a new function for scheduling the motionCheck right after // Make MotionCheck - CareKitTask like this:

  static func makeMotionCheck(date: Date) -> OCKTask {
    let thisMorning = Calendar.current.startOfDay(for: date)
    let nextWeek = Calendar.current.date(
      byAdding: .weekOfYear,
      value: 1,
      to: date
    )
    let nextMonth = Calendar.current.date(
      byAdding: .month,
      value: 1,
      to: thisMorning
    )
    let dailyElement = OCKScheduleElement(
      start: thisMorning,
      end: nextWeek,
      interval: DateComponents(day: 1),
      text: nil,
      targetValues: [],
      duration: .allDay
    )
    let weeklyElement = OCKScheduleElement(
      start: nextWeek ?? Date(),
      end: nextMonth,
      interval: DateComponents(weekOfYear: 1),
      text: nil,
      targetValues: [],
      duration: .allDay
    )
    let schedule = OCKSchedule(
      composing: [dailyElement, weeklyElement]
    )
    let task = OCKTask(
      id: TaskModel.motionCheck.rawValue,
      title: "Range Of Motion",
      carePlanUUID: nil,
      schedule: schedule
    )
    return task
  }

Here You made a custom scheduling for motion check; the schedule is you show the task right after vaccination for a week every day, then you show it one time per week for a month; that’s a good schedule, right?

Next step is to make sure you add it to the StoreManager; let’s open TaskViewModel, then inside of Vaccination case, add the motion check right after // Add MotionCheck comment.

,
                    // Add MotionCheck
                    TaskManager.makeMotionCheck(date: answer)

Here you add the motion check by using the date of vaccination.

Next is to create OCKSurveyTaskViewController, add a new case here since Xcode already complain about it right after // Make MotionCheck ViewController

    case .motionCheck:
      let viewController = OCKSurveyTaskViewController(
        taskID: TaskModel.motionCheck.rawValue,
        eventQuery: OCKEventQuery(for: date),
        storeManager: storeManager,
        survey: SurveyManager.motionSurvey(),
        extractOutcome: { _ in return [OCKOutcomeValue(Date())] })
      viewController.surveyDelegate = delegate
      viewController.view.isUserInteractionEnabled = !isFuture
      viewController.view.alpha = isFuture ? 0.4 : 1.0
      listViewController.appendViewController(viewController, animated: false)

As you’ve already done this step multiple times, you are just added a new OCKSurveyTaskViewController for the motion survey.

So, the last step for showing motion check is to navigate to TaskViewController and add the motion check id to the valid list of ids shown in the daily page view controller right after // Add MotionCheck id comment.

if id == TaskModel.checkIn || id == TaskModel.secondVaccinationCheck || id == TaskModel.motionCheck 

Here you modified the list of valid IDs in the daily page view controller. And that’s it; before building the project, make sure you delete the app since this task is also dependent on the vaccination task, then build and run to see how it look. Remember, this is an active task; if you want to try it out, you should build it on your phone.