2017-08-14 5 views
0

このチュートリアル(https://www.raywenderlich.com/86336/ios-8-healthkit-swift-getting-started)に従っていますが、別のHKQuantityTypeIdentifierが必要です。 HealthKitManagerクラスのコード:Swift: '次のタイプを共有するための許可が許可されていません:HKQuantityTypeIdentifierAppleExerciseTime'

import Foundation 
import UIKit 
import HealthKit 

class HealthKitManager { 

let healthKitStore:HKHealthStore = HKHealthStore() 

func authorizeHealthKit(completion: ((_ success:Bool, _ error:NSError?) -> Void)!) 
{ 


    let healthKitTypesToWrite: Set<HKSampleType> = [ 
     HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.appleExerciseTime)! 
    ] 
    let healthKitTypesToRead: Set<HKObjectType> = [ 
     HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.appleExerciseTime)! 
    ] 


    // If the store is not available (for instance, iPad) return an error and don't go on. 
    if !HKHealthStore.isHealthDataAvailable() 
    { 
     let error = NSError(domain: "com.example", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"]) 
     if(completion != nil) 
     { 
      completion?(false, error) 

     } 
     return; 
    } 
    healthKitStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) -> Void in 

     completion?(success, error! as NSError) 
    } 

} 

}

とのViewControllerでhealthKit呼び出そうと:私はエラーを取得していますauthorizeHealthKitを呼び出すときしかし

let healthManager:HealthKitManager = HealthKitManager() 


func authorizeHealthKit() { 
    print("1") 
    healthManager.authorizeHealthKit { (authorized, error) -> Void in 
     if authorized { 
      print("HealthKit authorization received.") 
     } 
     else 
     { 
      print("HealthKit authorization denied!") 
      if error != nil { 
       print("\(error)") 
      } 
     } 
    } 
} 

: 'をNSInvalidArgumentException'、理由 ' HKQuantityTypeIdentifierAppleExerciseTime 'は、次のタイプの共有を許可していません。印刷 "1"ステートメントは、クラッシュの前に呼び出されます。

答えて

0

あなたのアプリはHKQuantityTypeIdentifier.appleExerciseTimeと書くことが許可されていません。そのタイプの定義はアップル独自のものです。 HKQuantityTypehealthKitTypesToWriteから削除してください。

+0

応答してくれてありがとう、私はこれが事実かもしれないと思ったが、それをサポートするどんなドキュメンテーションも見ることができなかった。したがって、healthKitを使用してappleExerciseTimeを記録または追加する方法はありませんか?そうでない場合は、あなたはどのようにユーザーのトレーニングを記録することができます知っていますか? –

関連する問題