Swift - HealthKit 不会验证
Swift - HealthKit will not authenticate
我正在尝试制作一个基本脚本,它将在 iOS 上从 HealthKit 提取我的每日步数,但是当页面被触发时 - 没有任何反应。它应该请求读取 HealthKit 数据的权限,但我不知道哪里出错了。
这是我的代码:
import Foundation
import UIKit
import HealthKit
class HealthKitPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
class HealthKitManager {
let HealthStore = HKHealthStore()
func AuthoriseHealthKit() -> Bool {
var isEnabled = true
if HKHealthStore.isHealthDataAvailable() {
let StepCount = NSSet(object: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount))
let DataTypesToWrite = NSSet(object: StepCount)
let DataTypesToRead = NSSet(object: StepCount)
HealthStore.requestAuthorization(toShare: nil, read: (StepCount as! Set<HKObjectType>)) {
(success, error) -> Void in
isEnabled = success
}
}else{
isEnabled = false
}
return isEnabled
}
}
谁能提供一些建议?
import UIKit
import HealthKit
class HealthKitPage : UIViewController
{
let healthStore: HKHealthStore = HKHealthStore()
override func viewDidLoad()
{
var shareTypes = Set<HKSampleType>()
shareTypes.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!)
var readTypes = Set<HKObjectType>()
readTypes.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!)
healthStore.requestAuthorizationToShareTypes(shareTypes, readTypes: readTypes) { (success, error) -> Void in
if success {
print("success")
} else {
print("failure")
}
if let error = error { print(error) }
}
}
}
我能够使用 Himali 的答案解决这个问题,但它需要转换为 Swift3,这是我的工作文件:
import Foundation
import UIKit
import HealthKit
import UIKit
import HealthKit
class HealthKitPage : UIViewController
{
let healthStore: HKHealthStore = HKHealthStore()
override func viewDidLoad()
{
var shareTypes = Set<HKSampleType>()
shareTypes.insert(HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!)
var readTypes = Set<HKObjectType>()
readTypes.insert(HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!)
healthStore.requestAuthorization(toShare: shareTypes, read: readTypes) { (success, error) -> Void in
if success {
print("success")
} else {
print("failure")
}
if let error = error { print(error) }
}
}
我正在尝试制作一个基本脚本,它将在 iOS 上从 HealthKit 提取我的每日步数,但是当页面被触发时 - 没有任何反应。它应该请求读取 HealthKit 数据的权限,但我不知道哪里出错了。
这是我的代码:
import Foundation
import UIKit
import HealthKit
class HealthKitPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
class HealthKitManager {
let HealthStore = HKHealthStore()
func AuthoriseHealthKit() -> Bool {
var isEnabled = true
if HKHealthStore.isHealthDataAvailable() {
let StepCount = NSSet(object: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount))
let DataTypesToWrite = NSSet(object: StepCount)
let DataTypesToRead = NSSet(object: StepCount)
HealthStore.requestAuthorization(toShare: nil, read: (StepCount as! Set<HKObjectType>)) {
(success, error) -> Void in
isEnabled = success
}
}else{
isEnabled = false
}
return isEnabled
}
}
谁能提供一些建议?
import UIKit
import HealthKit
class HealthKitPage : UIViewController
{
let healthStore: HKHealthStore = HKHealthStore()
override func viewDidLoad()
{
var shareTypes = Set<HKSampleType>()
shareTypes.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!)
var readTypes = Set<HKObjectType>()
readTypes.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!)
healthStore.requestAuthorizationToShareTypes(shareTypes, readTypes: readTypes) { (success, error) -> Void in
if success {
print("success")
} else {
print("failure")
}
if let error = error { print(error) }
}
}
}
我能够使用 Himali 的答案解决这个问题,但它需要转换为 Swift3,这是我的工作文件:
import Foundation
import UIKit
import HealthKit
import UIKit
import HealthKit
class HealthKitPage : UIViewController
{
let healthStore: HKHealthStore = HKHealthStore()
override func viewDidLoad()
{
var shareTypes = Set<HKSampleType>()
shareTypes.insert(HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!)
var readTypes = Set<HKObjectType>()
readTypes.insert(HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!)
healthStore.requestAuthorization(toShare: shareTypes, read: readTypes) { (success, error) -> Void in
if success {
print("success")
} else {
print("failure")
}
if let error = error { print(error) }
}
}