技术控

    今日:5| 主题:57797
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] CareKit Tutorial for iOS: Part 1

[复制链接]
萌城少年 投递于 2016-10-4 02:32:22
334 3

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-1-技术控-understand,framework,important,continued,software
   Learn how easy it is to build a personal health app with CareKit!
    In a zombie apocalypse (or any other time, really) there’s nothing as important as health. Thankfully we have CareKit, an open source framework designed for building iOS apps that help users manage and understand their personal health. CareKit apps help users create and follow health plans, track progress, analyze the resulting data and even share that data with care providers.
  Thanks to Apple’s continued interest in improving personal health with open source software, along with the health tracking capabilities of the iPhone and Apple Watch, it’s never been easier to put together an app that can have a powerful impact on the lives of many people!
  In this CareKit tutorial, you’ll leverage CareKit in an app aimed at users trying to stay infection-free during a zombie apocalypse. You’ll create a care plan consisting of treatments to minimize risk and assessments to track signs of infection. You’ll also learn how integrating ResearchKit and HealthKit can make CareKit much more powerful.
   If you’re unfamiliar with ResearchKit tasks, you may benefit from reviewing ourResearchKit tutorial before starting.
  In Part 1 of the CareKit tutorial you’ll work on Zombie Training and Symptom Tracker; Part 2 will cover Insights and Connect.
   Grab some … err … your brains, and let’s dive in!
  Getting Started

   Download the ZombieKit starter project and open it in Xcode. This contains a stubbed-out Tab Bar Controller, ResearchKit and HealthKit, plus a few helpers you’ll learn about during this CareKit tutorial.
  Build and run ZombieKit to see the placeholder Tab Bar Controller in all its glory.
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-2-技术控-understand,framework,important,continued,software

   Note : The ResearchKit build in this starter is a pre-release version with Swift 3 support. As a result, you’ll see a few compile warnings on the ResearchKit target. Don’t worry about them—they won’t impact your work in this tutorial.
   First, add the CareKit framework to your project. Head over to the CareKit repository on GitHub and download the stable_xcode80_candidate zip or clone that branch using the following terminal command:
   
  1. git clone -b stable_xcode80_candidate --recurse-submodules https://github.com/carekit-apple/carekit.git
复制代码
    Note : As of this writing, stable_xcode80_candidate is your best bet for CareKit with Swift 3 and Xcode 8 compatibility. You’ll want to use stable , once Swift 3 changes make it there.
   Drag the downloaded CareKit.xcodeproj from Finder into the Project Navigator under the Linked Frameworks group. It should look like this:
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-3-技术控-understand,framework,important,continued,software

   Note: Don’t try to add a framework to a project while another Xcode project using that framework is open, because it won’t properly import. If your end result doesn’t look like the screenshot, close any other open projects and try again.
   Select the ZombieKit target, then the General tab of the target editor. Add CareKit.framework under the Embedded Binaries section.
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-4-技术控-understand,framework,important,continued,software

   To confirm that you’ve successfully added CareKit to the project, add the following to the top of TabBarViewController.swift :
   
  1. import CareKit
复制代码
   Build and run. If the project builds successfully, you’re all set!
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-5-技术控-understand,framework,important,continued,software

  Anatomy of a CareKit App

  Functionally, CareKit consists of four main UI modules:
  
       
  • Care Card defines and tracks actions meant to positively impact the user’s health. For example, a weight loss app might track minutes spent exercising each day.   
  • Symptom and Measurement Tracker defines and tracks symptoms or vitals. For instance, that weight loss app would track your weight each day.   
  • Insights allows you to chart the collected data as well as present written alerts or findings. The weight loss app could chart exercise against weight to help visualize the correlation.   
  • Connect enables communication with care providers and friends. The chart showing weight loss compared to exercise efforts could, for example, be put into PDF format and emailed to your physician.  
   Note: Remember, you’ll only work on Care Card and Symptom and Measurement Tracker in Part 1 of this CareKit tutorial. You’ll add Insights and Connect modules in Part 2.
  Care Plan Store

   The core of a CareKit app is the Care Plan Store , which manages and provides an interface to a database that persists the care plan. Its data are mapped to any connected Care Card and Symptom and Measurement Tracker controllers. When information is entered in views owned by these controllers, the Care Plan Store automatically picks it up.
  The store handles two primary data types you’ll use throughout this CareKit tutorial:
  
       
  • Activities represent the activities users will perform to manage their care and track their condition. These are often referred to collectively as the care plan.   
  • Events represent an individual occurrence of a scheduled activity. They are made up of the activity itself along with scheduling data, completion status and results when applicable.  
  Activities are further broken down into two types:
  
       
  • Intervention Activities are actions users take as part of their treatment. They appear on the Care Card.   
  • Assessment Activities represent activities users take within the app to evaluate their health. These appear on the Symptom and Measurement Tracker.  
   You might have noticed that the Connect module wasn’t mentioned in the store. It actually doesn’t live there; instead, it’s up to you to persist OCKContact objects on your own.
  The below chart shows the flow of information between the store, your contacts and the primary UI modules:
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-6-技术控-understand,framework,important,continued,software

   Now that you’ve got the basics down, it’s time to set up the Care Plan Store so it persists training and tracking data in ZombieKit. Open CarePlanStoreManager.swift and replace import Foundation with:
   
  1. import CareKit
复制代码
   You’ll use CareKit in this file, and it already includes Foundation.
   Add the following property to the top of the CarePlanStoreManager class:
   
  1. var store: OCKCarePlanStore
复制代码
    Next, add the following inside init() , just above super.init() :
   
  1. store = OCKCarePlanStore(persistenceDirectoryURL: storeURL)
复制代码
    This creates a Care Plan Store at storeURL in your documents directory. Note that the starter project includes a type property singleton called sharedCarePlanStoreManager —this allows you to access the store from anywhere in ZombieKit.
  Build and run to confirm a successful build. You won’t notice any differences until you add data and start hooking up CareKit controllers.
   
  Care Card

   In a zombie apocalypse, your users need to stay sharp. A great first step is to define a training plan and track adherence to it. CareKit has just the thing you need—the Care Card !
   Open TabBarViewController.swift and add the following property to the top of the TabBarViewController class:
   
  1. fileprivate let carePlanStoreManager = CarePlanStoreManager.sharedCarePlanStoreManager
复制代码
    This provides a pointer to the store you created in CarePlanStoreManager .
   Find createCareCardStack() and replace this line:
   
  1. let viewController = UIViewController()
复制代码
   With this:
   
  1. let viewController = OCKCareCardViewController(carePlanStore: carePlanStoreManager.store)
  2. viewController.maskImage = UIImage(named: "heart")
  3. viewController.smallMaskImage = UIImage(named: "small-heart")
  4. viewController.maskImageTintColor = UIColor.darkGreen()
复制代码
    This initializes an OCKCareCardViewController , the main controller for the Care Card, and passes a Care Plan Store to act as the data source. You also set the maskImage , smallMaskImage and maskImageTintColor with images and colors from the starter project that help zombify the look of the progress indicator. If these aren’t set, the Care Card progress image defaults to a red heart with red fill.
  Build and run—your Care Card should show a header with weekly navigation and progress as well as a detail view with a larger progress indicator. Right now both show 100% because you’ve completed 100% of your non-existent Intervention Activities. You’re great at doing nothing!
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-7-技术控-understand,framework,important,continued,software

  Preparing Care Plan Data

   Now you need to generate some care plan data, which will require a bit of setup. Select the Care Plan Store group in the Project Navigator and select File\New\File… , then choose the iOS\Source\Swift File template and click Next . Name the file CarePlanData.swift and click Create . In the new file, replace the template code with the following:
   
  1. import CareKit
  2. enum ActivityIdentifier: String {
  3.   case cardio
  4.   case limberUp = "Limber Up"
  5.   case targetPractice = "Target Practice"
  6.   case pulse
  7.   case temperature
  8. }
  9. class CarePlanData: NSObject {
  10.   let carePlanStore: OCKCarePlanStore
  11.   init(carePlanStore: OCKCarePlanStore) {
  12.     self.carePlanStore = carePlanStore
  13.     //TODO: Define intervention activities
  14.     //TODO: Define assessment activities
  15.     super.init()
  16.     //TODO: Add activities to store
  17.   }
  18. }
复制代码
    This code creates an enum, ActivityIdentifier , that will provide unique identifiers for intervention and assessment activities. It then defines a stubbed-out CarePlanData class which will be tasked with defining activities and adding them to the data store. Its initializer currently grabs a reference to OCKCarePlanStore for this purpose.
   To initialize an OCKCarePlanActivity to define an activity, you need to provide a schedule minimally defining a start date and frequency. Add the following to CarePlanData just below the properties:
   
  1. class func dailyScheduleRepeating(occurencesPerDay: UInt) -> OCKCareSchedule {
  2.   return OCKCareSchedule.dailySchedule(withStartDate: DateComponents.firstDateOfCurrentWeek,
  3.                                        occurrencesPerDay: occurencesPerDay)
  4. }
复制代码
    This method calls the convenience initializer dailySchedule(withStartDate:occurrencesPerDay:) , which creates an OCKCareSchedule with the given start date and number of daily occurrences. For the start date, you’re using NSDateComponents.firstDateOfCurrentWeek which is a computed property included with the starter project. This will generate results to cover the current week.
  Intervention Activities

   Now you’re ready to create some intervention activities. Replace //TODO: Define intervention activities with:
   
  1. import CareKit0
复制代码
    This code allocates three OCKCarePlanActivity objects of type intervention , which correlate to your Care Card. Each has a unique identifier based on your enum along with the title, instructions, tint color and schedule created with the helper. Soon you’ll be able to view where each of these fields map.
   To get the activities to appear in your Care Card View Controller, add them to the store by adding the following method to the bottom of CarePlanData :
   
  1. import CareKit1
复制代码
   What this code does:
  
       
  • You call activity(forIdentifier:completion:) to search the store for an activity with the given identifier. This is required to avoid inserting items with duplicate identifiers.   
  • If the fetch is successful, fetchedActivity unwraps; since there’s nothing to add, you return.   
  • If the identifier didn’t exist, you call add(_:completion:) on the store to add the activity to the carePlanStore .  
   Now you can use that code to add each of the activities you defined. Back in init(carePlanStore:) , replace //TODO: Add activities to store with:
   
  1. import CareKit2
复制代码
   This iterates through the activities you defined and adds each one to the store.
   You need to initialize this data class so the process to add data to the store will begin, so head over to TabBarViewController.swift and add the following property at the top of TabBarViewController :
   
  1. import CareKit3
复制代码
    Now add the following to init(coder:) , before the call to super :
   
  1. import CareKit4
复制代码
    This kicks off the initialization you wrote for CarePlanData that adds activities to the store.
   Note: Hard-coding activities will likely be used in some cases, but many CareKit apps will take other routes. Plans might be generated dynamically based on responses to a guided tour or derived from curated health plans maintained on a web service.
  Build and run—you’ve got yourself a zombie apocalypse training plan! Tap any bubble to fill it in, indicating you’ve completed one instance (or event) of the activity, and Care Completion updates accordingly. Tap elsewhere on the cell to see the instructions you provided for that activity.
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-8-技术控-understand,framework,important,continued,software

  After a modest amount of code, mostly to seed the data source, you now have a UI that displays and updates persisted Care Card data. That’s great news for your zombie-apocalypse users!
  Symptom and Measurement Tracker

  Now that your users have a plan to avoid infection, it’s time to tackle symptom tracking to monitor their zombie status.
   While the Care Card tracks actions taken to improve or maintain a condition, the Symptom and Measurement Tracker monitors progress. For ZombieKit, that means checking for signs of infection—or (un)death.
   In TabBarViewController.swift , add the following property to the others at the top of TabBarViewController :
   
  1. import CareKit5
复制代码
    You’ll need a reference to the OCKSymptomTrackerViewController , as it will be used throughout this class.
   In createSymptomTrackerStack() , replace:
   
  1. let viewController = UIViewController()
复制代码
   with the following:
   
  1. import CareKit7
复制代码
    This initializes your OCKSymptomTrackerViewController while providing its datasource, the Care Plan Store. You also set the progressRingTintColor , which optionally colors the progress indicator. Finally, you store the reference in symptomTrackerViewController for use elsewhere.
   Build and run, and check out the Symptom Tracker tab. Similarly to the Care Card, you’ll see a weekly and detail progress indicator, currently set to 100% due to your lack of activities.
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-9-技术控-understand,framework,important,continued,software

  Assessment Activities

  As you might have guessed, it’s time to eat some brai … Rather, it’s time to add some assessment activities to the store!
   Open CarePlanData.swift and head over to init(carePlanStore:) . Replace the line //TODO: Define assessment activities with the following:
   
  1. import CareKit8
复制代码
    You’ve defined two OCKCarePlanActivity objects—one for pulse and one for temperature—using a convenience initializer that creates assessment activities. This required a couple parameters not used in the intervention activities created earlier:
  
       
  • resultResettable takes a Boolean that indicates whether the user can re-take the assessment. When turned off, once data is entered, there’s no covering it up if you’re a zombie.   
  • You place a dictionary in userInfo with the key "ORKTask" . The value is a ResearchKit task (ORKTask) created by calling factory methods provided in AssessmentTaskFactory , a struct included with the starter project. CareKit can leverage ResearchKit tasks for obtaining health data, and you’ll see shortly how these tasks are used when the user completes an assessment.  
   At the bottom of init(carePlanStore:) , add your new tasks to the array you loop through when calling addActivity() . It should now look like this:
   
  1. import CareKit9
复制代码
      Build and run, and your new activities will appear in the Symptom Tracker tab. You’re now at 0% progress, because you haven’t completed either assessment yet. Note that as long as it isn’t the first day of the week, you can select and view prior days. Future days are never selectable.
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-10-技术控-understand,framework,important,continued,software
    There’s no putting it off—you might as well check to see if you’ve become a zombie. Select one of the assessments and … nothing happens!
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-11-技术控-understand,framework,important,continued,software

  Handling Input

  On the Care Card, intervention activities have a binary input—the user simply taps an event on or off to indicate if it occurred. Assessments are more versatile; they can capture all types of data and thus require a variety of user interfaces. For this reason, you’re required to provide a view controller to handle assessment input.
   ResearchKit provides a number of options for handling input of health data, and CareKit is designed to work well with them. As you may recall from the ResearchKit with Swift tutorial , input is gathered with ORKTaskViewController objects which require ORKTask objects. You’ve already bundled a task in the userInfo of your assessment object—now it’s time to use it.
   Open TabBarViewController.swift and add the following to the bottom of the file:
   
  1. import CareKit0
复制代码
    The Symptom and Measurement Tracker view controller requires an OCKSymptomTrackerViewControllerDelegate that must implement symptomTrackerViewController(_:didSelectRowWithAssessmentEvent:) . This method is required to present a view controller for obtaining assessment data. It unwraps the ORKTask you stored in userInfo and uses that to initialize an ORKTaskViewController , then present it.
   Hop back up to createSymptomTrackerStack() and add the following just after viewController is allocated:
   
  1. import CareKit1
复制代码
    This sets the TabBarViewController as the delegate, which means the extension you just defined will be used when an assessment is selected.
   Build and run, then head to the Symptom Tracker tab. Select either Pulse or Temperature and…you’ll get a crash . If you look in the console, there will be a message similar to this:
   
  1. import CareKit2
复制代码
   This is because ResearchKit tasks rely on HealthKit, and you haven’t yet enabled that capability. You’ll get to that in a bit, but for now you need to add the plist entry to appease this check.
   Open Info.plist within ZombieKit and add the NSHealthShareUsageDescription key with the value I want to obtain your pulse and temperature data because zombies :
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-12-技术控-understand,framework,important,continued,software

  You’ll access HealthKit data later in this tutorial, at which point the user will be prompted for authorization including the description you just provided.
  Build and run, and once again select Pulse or temperature on the Symptom Tracker tab. Your ResearchKit controller now presents data from the task factory.
   Note : In the console, you might see a warning beginning with [ResearchKit][Warning] . This is because ResearchKit wants to check HealthKit for the data, and you haven’t set that up yet.
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-13-技术控-understand,framework,important,continued,software

   If you try to hit Done or Cancel then End Task , nothing happens. This is because an ORKTaskViewController requires a delegate callback to handle its results. Back in TabBarViewController.swift , add the following at the bottom of the file:
   
  1. import CareKit3
复制代码
   
       
  • This dismisses the task view controller in a defer block that will execute after handling the results.   
  • Upon successful completion, you unwrap symptomTrackerViewController.lastSelectedAssessmentEvent to gain access to the event tied to the task that triggered the current task flow. In a moment, you’ll use this reference to save the results to the store.  
   You can’t simply save a ResearchKit task result into CareKit—you need to first convert it. Open CarePlanStoreManager.swift and import ResearchKit:
   
  1. import CareKit4
复制代码
    Then add the following method to CarePlanStoreManager :
   
  1. import CareKit5
复制代码
   This converts a ResearchKit result into a CareKit result.
  
       
  • The ORKTaskResult s you created have a single step, so you pull the first and only one into firstResult . Next you grab the first and only question result of this step, which will contain the data you collected.   
  • You cast the result as a ORKNumericQuestionResult , which is the only result type your tasks collect. You then grab the answer and pass it as the value when creating and returning an OCKCarePlanEventResult .   
  • Remember that you only call this method when a task successfully completes, so if it doesn’t contain a value, you’re simply failing.  
   Return to TabBarViewController.swift and replace //TODO: convert ORKTaskResult to CareKit result and add to store with:
   
  1. import CareKit6
复制代码
    This passes your ResearchKit task result to buildCarePlanResultFrom(taskResult:) and gets back an OCKCarePlanEventResult . You pass that carePlanResult along with the current event to update(:_with:state:completion:) to commit it to the store. You print an error message if it was unsuccessful.
   Finally, head back to symptomTrackerViewController(_:didSelectRowWithAssessmentEvent:) and replace //TODO: Set a delegate with:
   
  1. import CareKit7
复制代码
   Now when the user submits a completed ResearchKit task, your new delegate method will be invoked to convert and save it.
  Build and run. Now when you complete an assessment, the task controller will dismiss, your care store will update, and the symptom tracker will update to reflect changes to the completion percent and recorded value.
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-14-技术控-understand,framework,important,continued,software

   Note : You’ll likely want to employ local notifications to notify users when it’s time to complete an activity. To learn more about creating local notifications, check out the Local and Remote Notification Programming Guide from Apple.
  HealthKit Integration

  As you’ve seen, CareKit is able to leverage ResearchKit to gather health data. Both of these frameworks also work closely with HealthKit, which makes it easy to access, transform and store health data. This enables powerful flows such as CareKit obtaining a pulse from HealthKit, which may have passively collected it from an Apple Watch or other device.
  Because you’re already using ResearchKit to gather this data, enabling the option to pull from HealthKit is as trivial as enabling HealthKit in your project. Just be warned—there’s no hiding the fact that you’re a zombie from your Watch. :]
   First, select the ZombieKit target, then the General tab. Set the bundle identifier to match your domain or naming convention, then select the Team associated with your development account. This is necessary so that Xcode can create an App ID with the appropriate entitlement.
   

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-15-技术控-understand,framework,important,continued,software

   With the target still selected, switch to the Capabilities tab. Flip the switch for HealthKit to enable it for your newly created app ID.
   Select ResearchKit.xcodeproj from the Linked Frameworks group, then the ResearchKit target. Set your team here for this target as well.
  Build and run, select the Symptom Tracker tab, and attempt one of the assessments. You will now see a prompt requesting HealthKit access, with the message you defined earlier, before the task controller is displayed. And if you’re running on a device that has pulse or temperature data stored, you’ll see the most recent reading automatically populate!
   Note : In the console, you may see a warning about Unbalanced calls to begin/end appearance transitions . This is a known ResearchKit issue that occurs on the automatic display of the initial permission request view controller. Unfortunately, it’s built into the ResearchKit framework, so it can’t be avoided.
     

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-16-技术控-understand,framework,important,continued,software
   HealthKit Permission Prompt
      

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-17-技术控-understand,framework,important,continued,software
   Data populated from HealthKit!
     Note : While it’s out of the scope of this CareKit tutorial, you could and likely would want to save assessment data to HealthKit. Since CareKit data is sandboxed within your app, saving it to HealthKit is your best bet for sharing with other apps. For more info, check out Sharing Data with HealthKit .
  Where To Go From Here?

   Download the completed code for this CareKit tutorialhere. Note that the final project does not have HealthKit enabled, since doing so requires access to your developer account. If you want to skip to the final project and wish to see HealthKit, follow the quick config steps in the HealthKit Integration section above.
   To learn about more ResearchKit tasks you can leverage for assessments, check out ResearchKit Tutorial with Swift: Getting Started and Accessing Heart Rate Data for Your ResearchKit Study . You can also review HealthKit Tutorial with Swift: Getting Started for a refresher on working with HealthKit.
   Stay tuned for Part 2 of this series, where you’ll build on ZombieKit to visualize the collected data via Insights and share it using Connect.
  We hope you enjoyed the CareKit tutorial and feel empowered to help people take control of their health. Please join the discussion about this CareKit tutorial and the coming zombie apocalypse below!

CareKit Tutorial for iOS: Part 1

CareKit Tutorial for iOS: Part 1-24-技术控-understand,framework,important,continued,software



上一篇:Sass Mixins For Your Next Project
下一篇:Top Certs to Advance Your IT Career
我是ゝ高富帥 投递于 2016-10-25 07:47:04
支持你哈...................................
回复 支持 反对

使用道具 举报

思萱 投递于 2016-10-27 00:45:06
世界末日我都挺过去了,看到萌城少年我才知道为什么上帝留我到现在!
回复 支持 反对

使用道具 举报

另一种晚安 投递于 2016-11-8 00:49:19
回萌城少年帖为“保增长、扩内需、调结构,促民生”作出贡献,顺便赚积分
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表