2017-12-30 10 views
0

Ich arbeite an einer Workout-App für die Apple Watch und stieß auf einige Probleme mit HealthKit auf meiner tatsächlichen Uhr.HealthKit Genehmigung gewährt und arbeitet am Simulator, aber nicht am tatsächlichen Gerät

Das Anfordern der Autorisierung funktioniert sowohl auf dem Simulator als auch auf meinem Gerät und wird bei jedem Start als erfolgreich angezeigt. Das Abfragen und Lesen der Samples schlägt jedoch auf meinem Gerät fehl, nicht jedoch auf dem Simulator.

Nach dem Start, mit erfolgreicher Autorisierung, wenn es das Training abfragen oder speichern muss, heißt es "Autorisierung ist nicht festgelegt".

Beide Berechtigungen haben HealthKit auf YES gesetzt, HealthKit und Hintergrundfunktionen sind eingeschaltet, NSHealthShareUsageDescription und NSHealthUpdateUsageDescription Schlüssel sind in der iOS Info.plist enthalten.

Autorisierungscode

// 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 ?? "")") 
     } 
    } 

Abfragecode (Udemy Kurs)

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) 
} 

Antwort

1

habe ich den Autorisierungscode in meinem ExtensionDelegate anstelle der gleichen Datei, wo ich abfragt und es begann zu arbeiten.

Immer noch seltsam, dass es auf dem Simulator aber nicht auf einem tatsächlichen Gerät vor arbeitete.

Verwandte Themen