2015-10-08 51 views
8

ヒートキットがユーザーのデータを読み取ることが許可されているかどうかを確認したい場合、しかし、requestAuthorizationToShareTypesは常にtrueを返すようですか?ユーザーが私を許可したかどうかの参照を取得するにはどうすればよいですか?HealthKitが承認されているかどうかを確認する方法

override func viewDidLoad() { 
     super.viewDidLoad() 

     //1. Set the types you want to read from HK Store 
     let healthKitTypesToRead: [AnyObject?] = [ 
      HKObjectType.workoutType() 
     ] 


     //2. 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.myndarc.myrunz", code: 2, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available in this Device"]) 
       print(error) 

      let alertController = UIAlertController(title: "HealthKit Not Available", message: "It doesn't look like HealthKit is available on your device.", preferredStyle: .Alert) 
      presentViewController(alertController, animated: true, completion: nil) 
      let ok = UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in }) 
      alertController.addAction(ok) 
        } 

     //3. Request Healthkit Authorization 

     let sampleTypes = Set(healthKitTypesToRead.flatMap { $0 as? HKSampleType }) 

     healthKitStore.requestAuthorizationToShareTypes(sampleTypes, readTypes: nil) { 

      (success, error) -> Void in 

      if success { 
       dispatch_async(dispatch_get_main_queue(), {() -> Void in 
                 self.performSegueWithIdentifier("segueToWorkouts", sender: nil) 
                }); 
      } else { 
       print(error) 
       dispatch_async(dispatch_get_main_queue(), {() -> Void in 
             self.showHKAuthRequestAlert() 
            }); 
      } 

     } 
    } 

代わりに、私はauthorizationStatusForTypeを試してみましたが、その列挙値に切り替えるが、私は常に許可てるという点で、同じ問題を抱えていました。

+0

におけるアクセス許可のアクセスを要求し、チェックの例のシステムは、許可についてあなたを尋ねたことがありませんか? –

+0

いいえ、それはありませんが、あなたはそれを許可していない場合でも、成功のブロックは – GarySabo

答えて

6

このコンテキストでは、successフラグが意味することを誤解しています。 successが真の場合、すべてのことは、iOSがヘルスキットのアクセスについてユーザーに問われたことを意味します。それは彼らがその質問に「はい」と答えたことを意味するものではありません。

「はい/いいえ」と答えるかどうかを判断するには、特定の種類のデータを読み書きする権限があるかどうかを特定する必要があります。 HealthKitのapple docs :

承認を申請したら、あなたのアプリはHealthKitストアにアクセスする準備が整いました。あなたのアプリケーションがデータ型を共有する権限を持っている場合、その型のサンプルを作成して保存することができます。サンプルを保存しようとする前に、あなたのアプリケーションがデータを共有する権限を持っていることを確認する必要があります。ここで

+3

私はauthorizationStatusForTypeを試しました...ありがとうと呼ばれているが、私は理解して私がこれを読むことができませんから。http://stackoverflow.com/質問/ 25512320/healthkit-hkauthorization-for-reading-data – GarySabo

+2

アップルがユーザーのプライバシーを気にしています。あなたに許可が与えられていない場合、要求されたタイプのデータがHealthKitストアにないかのように見えます。あなたのアプリに共有権限が与えられているが、読み取り権限が与えられていない場合は、アプリがストアに書き込んだデータだけが表示されます。他のソースからのデータは隠されたままです。 – Aks

5

HealthKitStore

// Present user with items we need permission for in HealthKit 
healthKitStore.requestAuthorization(toShare: typesToShare, read: typesToRead, completion: { (userWasShownPermissionView, error) in 

    // Determine if the user saw the permission view 
    if (userWasShownPermissionView) { 
     print("User was shown permission view") 

     // ** IMPORTANT 
     // Check for access to your HealthKit Type(s). This is an example of using BodyMass. 
     if (self.healthKitStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!) == .sharingAuthorized) { 
      print("Permission Granted to Access BodyMass") 
     } else { 
      print("Permission Denied to Access BodyMass") 
     } 

    } else { 
     print("User was not shown permission view") 

     // An error occurred 
     if let e = error { 
      print(e) 
     } 
    } 
}) 
+0

'requestAuthorization'は認証ビューを表示します。これは*チェックではありません*。 – Ixx

関連する問題