标签:
import WatchKit import Foundation import HealthKit import ClockKit class ExtensionDelegate: 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 applicationDidBecomeActive() { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. gameData.getUserData() gameData.startBenefitTimer() gameData.checkPetReward() gameData.checkFatAdding() gameData.checkHealthReward() gameData.startWorkSession() } func applicationWillResignActive() { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, etc. gameData.setUserData() gameData.closeBenefitTimer() gameData.endWorkSession() // refreshComplications() } func didReceiveLocalNotification(notification: UILocalNotification) { } func firstInit() { // --- 标示用户是否为首次登陆 userDefault.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() { // --- 初始化csv数据 csvReader.initDictionaryData() // --- 请求healthkit授权 print("start request authorize") healthManager.authorizeHealthKit { (authorized, error) -> Void in if authorized { print("HealthKit authorization received.") } else{ print("HealthKit authorization denied!") if error != nil { print("\(error)") } } } } func refreshComplications() { let clockServer = CLKComplicationServer.sharedInstance() for complication in clockServer.activeComplications { if complication.family == .ModularLarge { // refresh the complication datas clockServer.extendTimelineForComplication(complication) } } } }
在此处对手机进行授权访问验证后,才能使apple watch 的healthkit 数据授权成功。
Watch OS 2.0 健身宠物app开发心得[1]-Healthkit的sdk接入
标签:
原文地址:http://www.cnblogs.com/hutong/p/4822252.html