HealthKit授权授予并在模拟器上工作,但不在实际设备上

问题描述 投票:0回答:1

我正在为Apple Watch制作锻炼应用程序,并在我的实际手表上使用HealthKit遇到了一些问题。

请求授权适用于模拟器和我的设备,并在每次启动时显示成功。然而,查询和读取样本在我的设备上失败但在模拟器上失败。

启动后,成功授权,当需要查询或保存锻炼时,它会显示“授权未确定”。

这两个权利都将HealthKit设置为YES,HealthKit和后台功能已打开,iOS Info.plist中提供了NSHealthShareUsageDescription和NSHealthUpdateUsageDescription键。

授权码

// Configure write values
    let writeTypes: Set<HKSampleType> = [.workoutType(),
                                         HKSampleType.quantityType(forIdentifier: .heartRate)!,
                                         HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!,
                                         HKSampleType.quantityType(forIdentifier: .stepCount)!,
                                         HKSampleType.quantityType(forIdentifier: .distanceCycling)!,
                                         HKSampleType.quantityType(forIdentifier: .distanceSwimming)!,
                                         HKSampleType.quantityType(forIdentifier: .distanceWalkingRunning)!,
                                         HKSampleType.quantityType(forIdentifier: .swimmingStrokeCount)!]
    // Configure read values
    let readTypes: Set<HKObjectType> = [.activitySummaryType(), .workoutType(),
                                        HKObjectType.quantityType(forIdentifier: .heartRate)!,
                                        HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
                                        HKObjectType.quantityType(forIdentifier: .stepCount)!,
                                        HKObjectType.quantityType(forIdentifier: .distanceCycling)!,
                                        HKObjectType.quantityType(forIdentifier: .distanceSwimming)!,
                                        HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!,
                                        HKObjectType.quantityType(forIdentifier: .swimmingStrokeCount)!]

    // Create health store
    let healthStore = HKHealthStore()

    // Use it to request authorization for our types
    healthStore.requestAuthorization(toShare: writeTypes, read: readTypes) { (success, error) in
        if success {
            print("Success: authorization granted")
        } else {
            print("Error: \(error?.localizedDescription ?? "")")
        }
    }

查询代码(Udemy课程)

func startQuery(_ quantityTypeIdentifier: HKQuantityTypeIdentifier) {
    // We only want data points after our workout start date
    let datePredicate = HKQuery.predicateForSamples(withStart: workoutStartDate, end: nil, options: .strictStartDate)
    // And from our current device
    let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
    // Combine them
    let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [datePredicate, devicePredicate])
    // Write code to receive results from our query
    let updateHandler: (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void = { query, samples, deletedObjects, queryAnchor, error in

        //safely typecast to a quantity sample so we can read values
        guard let samples = samples as? [HKQuantitySample] else { return }

        //process the samples
        print("Start processing samples")
        self.process(samples, type: quantityTypeIdentifier)
    }

    // Create the query out of our type (e.g. heart rate), predicate and result handling code
    let quantityType = HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!
    let query = HKAnchoredObjectQuery(type: quantityType, predicate: queryPredicate, anchor: nil, limit: HKObjectQueryNoLimit, resultsHandler: updateHandler)

    // Tell HealthKit to re-run the code every time new data is available
    query.updateHandler = updateHandler

    // Start the query running
    healthStore.execute(query)

    // Stach it away so we can stop it later
    activeDataQueries.append(query)
}
ios swift watchkit health-kit
1个回答
1
投票

我将授权代码放在我的ExtensionDelegate中,而不是我查询的同一个文件,它开始工作。

尽管如此,它仍然很奇怪它在模拟器上工作但在实际设备上却没有。

© www.soinside.com 2019 - 2024. All rights reserved.