HealthKit开发教程:用Swift编写个健康应用!
时间 2015-01-23 18:13:44
CSDN移动开发
原文
http://www.csdn.net/article/2015-01-23/2823686-healthkit-tutorial-with-swift
一、起步 HealthKit是iOS 8中的新的API,它提供了一种优雅的方式来获取和存储用户的健康数据。 在本篇HealthKit教程中,你将会创建一个简单地记录用户信息的App。在此过程中,你会学到许多关于HealthKit的知识,例如:
准备好进行一次精彩的HealthKit之旅了吗?继续往下读吧! 注意:要想完成这次教程,你应该有一个可用的iOS开发者账号,如果没有的话,你将无法让HealthKit工作,也没有访问HealthKit Store的权限。 开始你将创建一款简单地应用,获取使用HealthKit的许可,然后读写HealthKit的数据。这里为你准备了一款起始项目,该起始项目中已经创建好了所有的用户交互界面,你只需要将注意力集中于HealthKit功能即可。 现在下载这个 初始项目 ,然后在Xcode中打开。 编译并且运行这个工程,你会看到这款应用的内部结构:读写用户锻炼信息(Workout)与身体数据采样的信息(Quantity samples)。
接下来,你将按照如下顺序完善这款应用:
在这之前,你必须先更改这个项目的Bundle Identifier,然后选择你的开发团队Team。 在项目导航栏中选择我们的项目HKTutorial,在Target栏中选择HKTutorial。然后选择General菜单,将Bundle Identifier改为你自己的名字或者是域名。 然后,在Team组合框中选择与你的开发者账号关联的开发团队。
到目前为止一切顺利! 授权为了使用HealthKit,你必须为HealthKit授权。 依然是在Target栏中,打开Capabilities菜单,将HealthKit这一部分的开关设为ON的状态,如屏幕截图中显示那样:
等待Xcode做好相关的配置,一旦Xcode完成配置,你的授权工作就完成了。这很简单,不是吗? 许可(Permissions)记住,你的应用永远不会自动获取健康数据——你需要获得许可。这就是接下来你要做的事情。 首先,打开HealthManager.swift,看一下,你会发现一个空的类。 在这里你将添加你的这个工程所需要的HealthKit相关的代码。它将是其他类与HealthKit交互的入口。一个好消息是,你已经在一些必要的控制器中有这个类的一个实例了,所以不需要再创建其他的实例了。 导入HealthKit框架,依然是在HealthManager.swift中,在顶部注释的下面加上这一行: import HealthKit
HealthKit框架的核心是HKHealthStore类,因此你也需要这个类的一个实例,在HealthManager中加入这一行: let healthKitStore:HKHealthStore = HKHealthStore()
既然你已经创建好了HKHealthStore类的实例,下一步就是获得许可使用它了。 还记得吗?用户是可以掌控他们的数据并决定哪一部分可以被你记录追踪的。这意味着你并不是去一次性请求HealthKit Store的全局许可,而是去获取某一特定的类型的许可,是你的应用需要从Store中读写的那一部分。 所有目标均继承自HKObjectType类,该类提供了方便的方法来创建子类。 你只需要调用一个方法,传入一个代表特定种类的请求的常量即可,下面列出了这些方便的方法,覆盖了上面提到过的每一种类别。这里不需要在Xcode中做任何事,只需要看一看,学习一下: class func quantityTypeForIdentifier(identifier: String!) -> HKQuantityType! // to get a Quantity Type class func categoryTypeForIdentifier(identifier: String!) -> HKCategoryType! // to get a Category Type class func characteristicTypeForIdentifier(identifier: String!) -> HKCharacteristicType! // to get a Characteristic type class func correlationTypeForIdentifier(identifier: String!) -> HKCorrelationType! // to get a CorrelationType class func workoutType() -> HKWorkoutType! // to get a Workout type 在这些方法中使用到的标识符必须是在HealthKit中预定义的常量,例如HKQuantityTypeIdentifierHeight是数据采样信息种类中的身高测量,HKCharacteristicTypeIdentifierBloodType是个人特征种类中的血液类型。 锻炼信息不需要任何标识符,因为它并没有子类别。 现在回到编码上来,打开HealthManager.swift,将下面这个方法添加到HealthManager中。 func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!) { // 1. Set the types you want to read from HK Store let healthKitTypesToRead = NSSet(array:[ HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth), HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType), HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex), HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass), HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight), HKObjectType.workoutType() ]) // 2. Set the types you want to write to HK Store let healthKitTypesToWrite = NSSet(array:[ HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex), HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned), HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning), HKQuantityType.workoutType() ]) // 3. If the store is not available (for instance, iPad) return an error and don't go on. if !HKHealthStore.isHealthDataAvailable() { let error = NSError(domain: "com.raywenderlich.tutorials.healthkit", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"]) if( completion != nil ) { completion(success:false, error:error) } return; } // 4. Request HealthKit authorization healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in if( completion != nil ) { completion(success:success,error:error) } } } 让我们回顾一下上面的代码,一步一步来:
既然你的代码知道怎么样去请求许可,你需要为你的app提供一种方法来回调。 我们的起始项目中已经有一个“Authorize HealthKit”按钮来做这件事,它会在MasterViewController中调用authorizeHealthKit()方法。那里听起来像是一个非常好从你的app收到反馈的地方。 打开MasterViewController.swift,找到authorizeHealthKit()然后将下面这一行: println("TODO: Request HealthKit authorization") 替换为: healthManager.authorizeHealthKit { (authorized, error) -> Void in if authorized { println("HealthKit authorization received.") } else { println("HealthKit authorization denied!") if error != nil { println("\(error)") } } } 这段代码是从authorizeHealthKit发来的请求许可的回调,在控制台上用一段信息展示了请求结果。 编译然后运行,在主视图中点击“Authorize HealthKit”按钮,你将会看到这个场景弹出:
将所有的开关打开,然后点击Done按钮,你将会在Xcode中看到如下信息: HealthKit authorization received. 太棒了,你的应用已经成功连接到Store了,准备更深入到HealthKit的世界中 |
|