Watch Os2-healthkit SDK Access blogger in the end of July this year began to contact the watch os2.0 system, heard that in os2.0 has supported the development of the native app, it is interested in dash began a long trip to the pit! Bo Main development of the project is to develop a fitness pet game on Watch, such as: The main function is to use watch os2.0 on the Healkit SDK, according to watch collected personal health data, and complete the native app (that is, fitness pet, After the assignment, you can get the vitality of exercising your pet, as well as capturing the various kinds of pets. The evolution of your pet in captivity will also vary according to the results of your master's workout, which is divided into muscular, intelligent, and obese types. As a result of these features, the core module for developing this fitness pet is the HealthKit SDK on Apple Watch. During the development of the blogger, the development environment used was Xcode 7.0 Beta 5, watch OS 2.0, and iOS 9.0. OK, so, the following is to tell the blogger in the construction project stage encountered the first pit, is not only the Healkit SDK access, will meet the most important problem. First, to import the HealthKit SDK from Apple, you'll need to find the capabilities directory for iOS targets and WatchKit extensition under Target in your project project file. Open the HealthKit switch in both. The exact location is as follows: After opening the healkit switch of iOS and WatchKit extensition in the project settings, we will proceed to the next step-request authorization to HealthKit data read. There are a lot of similar use cases on the Internet, here I also will use my method to paste out, here is not the focus, then the setting in the Appdelegate.swift is the focus.
import watchkitimport foundationimport healthkitimport clockkitclassExtensiondelegate:nsobject, wkextensiondelegate{let Healthmanager=Healthmanager () let Userdefault=nsuserdefaults.standarduserdefaults () let GameData=gamedata.shareinstance () let Csvreader=csvreadersingleton.shareinstance () func applicationdidfinishlaunching () {//Perform Any final initialization of your application.initdata () gamedata.configdata= Gamedata.getconfigdatafromcsv (9000) if!userdefault.boolforkey ("Firstlogin") {firstinit () Gamedata.setuserdata ()} gamedata.getuserdata ()} func appli Cationdidbecomeactive () {//Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.Gamedata.getuserdata () Gamedata.startbenefittimer () Gamedata.checkpetreward () gamedata.ch Eckfatadding () Gamedata.checkhealthreward () Gamedata.startworksession ()} func Applicationwill Resignactive () {//Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state. //Use the This method to pause ongoing tasks, disable timers, etc.Gamedata.setuserdata () Gamedata.closebenefittimer () gamedata.endworksession ( )//refreshcomplications ()} func didreceivelocalnotification (notification:uilocalnotification) {} func Firstinit ( ) { //---Indicate if the user is first logged inUserdefault.setbool (true, Forkey:"Firstlogin") Gamedata.currentpetdata.ID=GameData.configData.defaultPet gameData.currentPlayerData.storeItemUsing=GameData.configData.defaultTrain GameData.currentPlayerData.storeItemPurchased.append ( GameData.configData.defaultTrain)} func InitData () {//---initializing CSV dataCsvreader.initdictionarydata ()//---request HealthKit authorizationPrint"Start request Authorize") Healthmanager.authorizehealthkit {(authorized, error)-Voidinch ifAuthorized {print ("HealthKit authorization received.") } Else{print ("HealthKit Authorization denied!") ifError! =Nil {print ("\ (Error)")}}}} func refreshcomplications () {Let clockserver=clkcomplicationserver.sharedinstance () forComplicationinchclockserver.activecomplications {ifcomplication.family = = . Modularlarge {//Refresh the complication datasclockserver.extendtimelineforcomplication (complication)}} }}
After obtaining authorization on the WatchKit extensition (that is, watch obtaining authorization), you also need to make a HealthKit authorization verification (unavoidable) to the phone. This is a step that many people tend to overlook, and without this step, Apple Watch is unable to get data from the HealthKit center. Therefore, we need to add the following sentence to the Appdelegate.swift in the iOS Project: ()
Authorization to access your phone in this place allows Apple Watch's HealthKit data to be licensed successfully.
Well, the first article is mainly to HealthKit access, the next blog, Bo will specifically describe how to achieve the heartbeat data reading, as well as Pulse beat animation, and share the Apple Watch on the hour minute to get HealthKit step data encountered in the pit, And how to fill this pit.
Watch OS 2.0 Fitness Pet App development experience [1]-healthkit SDK Access