无法使用类型

时间:2015-08-05 15:14:10

标签: ios swift swift2 xcode7 health-kit

它说:

Cannot invoke 'requestAuthorizationToShareTypes' with an argument list of type (HKQuantityType, readTypes: HKCharacteristicType, completion: (Bool, NSError!) -> Void)

请帮助

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if HKHealthStore.isHealthDataAvailable() {
        let healthStore = HKHealthStore()
        healthStore.requestAuthorizationToShareTypes(HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!, readTypes: (HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex))! , completion:{
            (success:Bool,error:NSError!) -> Void in
            if !success{
                print("error")
            }
        })
    }

}

2 个答案:

答案 0 :(得分:1)

requestAuthorizationForTypes方法需要HKObjectType。试试这个:

let shareTypes : Set = [HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
let readTypes : Set = [HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!]
healthStore.requestAuthorizationToShareTypes(shareTypes, readTypes:readTypes, completion: { (success, error) -> Void in
    if !success{
        print("error")
    }
})

答案 1 :(得分:0)

尝试这种方法

// MARK: - HealthKit
func performAutorizationForHealthKit(var completion:((success: Bool, error: NSError!) -> Void)?) {
    let healthKitTypesToShare: Set = [ HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
    if !HKHealthStore.isHealthDataAvailable() {
        let error = NSError(domain: "com.mutualCore.healthKit", code: 1, userInfo: [NSLocalizedDescriptionKey : "HealthKit is not available in this Device"])
        completion?(success: false, error: error)
        completion = nil
        return;
    }

    healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToShare, readTypes: [], completion: {
        (successRequest, errorRequest) -> Void in
        completion?(success: successRequest, error: errorRequest)
    })
}

用法

    performAutorizationForHealthKit { (success, error) -> Void in
        if error != nil {
            print("success")
        }
    }

开始RayWenderlich tutorial HealthKit

也很好