Apple Health Frameworks

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

Part 1: Essentials

03. Add an Onboarding Task

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: 02. Prepare a Project Next episode: 04. Learn More About CareKit

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.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Part 1, Episode 03, Start with Onboarding Task

In this episode, I want to show you how to add a single task to the app in order to show an onboarding step to the user.

case onboarding
let onboardingSchedule = OCKSchedule.dailyAtTime(
  hour: 0,
  minutes: 0,
  start: Date(),
  end: nil,
  text: "Due date is today!",
  duration: .allDay)
var onboardingTask = OCKTask(
  id: TaskModel.onboarding.rawValue,
  title: "Onboarding Task",
  carePlanUUID: nil,
  schedule: onboardingSchedule)
onboardingTask.instructions = "You have to review this task to be able to get access to the app!"
onboardingTask.impactsAdherence = false
return onboardingTask
let taskList = [TaskManager.makeOnboarding()]
    let welcomeInstructionStep = ORKInstructionStep(
      identifier: IdentifierModel.onboardingWelcome.rawValue
    )
    welcomeInstructionStep.title = "Welcome!"
    welcomeInstructionStep.detailText =
      "Thank you for attending to this course. Tap Next to learn more before start using."
    welcomeInstructionStep.image = UIImage(named: "welcome-image")
    welcomeInstructionStep.imageContentMode = .scaleAspectFill
    return welcomeInstructionStep
switch input {
case .onboarding:
  TaskViewModel.checkIfInputTaskIsComplete(input: input, storeManager: storeManager) { isComplete in
    if !isComplete {
      let viewController = OCKSurveyTaskViewController(
        taskID: input.rawValue,
        eventQuery: OCKEventQuery(for: date),
        storeManager: storeManager,
        survey: SurveyManager.onboardingSurvey(),
        extractOutcome: { _ in [OCKOutcomeValue(Date())] })
      viewController.surveyDelegate = delegate
      listViewController.appendViewController(viewController, animated: false)
    }
  }
}
TaskViewModel.makeTaskViewController(
  input: TaskModel.onboarding,
  date: date,
  storeManager: self.storeManager,
  listViewController: listViewController,
  delegate: self)
let overviewInstructionStep = ORKInstructionStep(
  identifier: IdentifierModel.onboardingOverview.rawValue
)
overviewInstructionStep.title = "Before You Start"
overviewInstructionStep.iconImage = UIImage(systemName: "checkmark.seal.fill")
let heartBodyItem = ORKBodyItem(
  text: "The app will ask you to share some of your health data.",
  detailText: nil,
  image: UIImage(systemName: "heart.fill"),
  learnMoreItem: nil,
  bodyItemStyle: .image
)
let completeTasksBodyItem = ORKBodyItem(
  text: "You will be asked to complete various tasks over the duration of using the app.",
  detailText: nil,
  image: UIImage(systemName: "checkmark.circle.fill"),
  learnMoreItem: nil,
  bodyItemStyle: .image
)
let signatureBodyItem = ORKBodyItem(
  text: "Before joining, we will ask you to sign an informed consent document.",
  detailText: nil,
  image: UIImage(systemName: "signature"),
  learnMoreItem: nil,
  bodyItemStyle: .image
)
let secureDataBodyItem = ORKBodyItem(
  text: "Your data is kept private and secure on your iPhone.",
  detailText: nil,
  image: UIImage(systemName: "lock.fill"),
  learnMoreItem: nil,
  bodyItemStyle: .image
)
overviewInstructionStep.bodyItems = [
  heartBodyItem,
  completeTasksBodyItem,
  signatureBodyItem,
  secureDataBodyItem
]
return overviewInstructionStep
}
let file = Bundle.main.path(forResource: "consent", ofType: "html") ?? ""
let html = try? String(contentsOfFile: file, encoding: String.Encoding.utf8)
let consentHTML = html ?? ""
let webViewStep = ORKWebViewStep(
  identifier: IdentifierModel.onboardingSignatureCapture.rawValue,
  html: consentHTML
)
webViewStep.showSignatureAfterContent = true
return webViewStep
var healthKitTypesToWrite: Set<HKSampleType> = []
if
  let fever = HKObjectType.categoryType(forIdentifier: .fever),
  let bodyTemperature = HKObjectType.quantityType(forIdentifier: .bodyTemperature) {
  healthKitTypesToWrite.insert(fever)
  healthKitTypesToWrite.insert(bodyTemperature)
}
var healthKitTypesToRead: Set<HKObjectType> = []
if let fever = HKObjectType.categoryType(forIdentifier: .fever),
  let bodyTemperature = HKObjectType.quantityType(forIdentifier: .bodyTemperature) {
  healthKitTypesToRead.insert(fever)
  healthKitTypesToRead.insert(bodyTemperature)
}
let healthKitPermissionType = ORKHealthKitPermissionType(
  sampleTypesToWrite: healthKitTypesToWrite,
  objectTypesToRead: healthKitTypesToRead
)
let notificationsPermissionType = ORKNotificationPermissionType(
  authorizationOptions: [.alert, .badge, .sound]
)
let motionPermissionType = ORKMotionActivityPermissionType()
let requestPermissionsStep = ORKRequestPermissionsStep(
  identifier: IdentifierModel.onboardingRequestPermissions.rawValue,
  permissionTypes: [
    healthKitPermissionType,
    notificationsPermissionType,
    motionPermissionType
  ]
)
requestPermissionsStep.title = "Health Data Request"
requestPermissionsStep.text =
  "Please review the health data types below and enable sharing to contribute to the app."
return requestPermissionsStep
	<key>NSHealthUpdateUsageDescription</key>
	<string>We need to have access to your health data for the study</string>
	<key>NSHealthShareUsageDescription</key>
	<string>We need to have access to your health data for the study</string>
	<key>NSMotionUsageDescription</key>
	<string>We need to have access to your health data for the study</string>
let completionStep = ORKCompletionStep(identifier: IdentifierModel.onboardingCompletion.rawValue)
completionStep.title = "Task Complete"
completionStep.text = "Thank you for starting this corse!"
return completionStep