public async Task Init() { if (HKHealthStore.IsHealthDataAvailable) { var success = await HealthStore.RequestAuthorizationToShareAsync(DataTypesToWrite, DataTypesToRead); /*if (!success.Item1) * { * Console.WriteLine("You didn't allow HealthKit to access these read/write data types. " + * "In your app, try to handle this error gracefully when a user decides not to provide access. " + * "If you're using a simulator, try it on a device."); * return; * }*/ } }
protected override async Task InitializeAsync() { await base.InitializeAsync(); if (HKHealthStore.IsHealthDataAvailable) { NSSet objectTypesToRead = NSSet.MakeNSObjectSet <HKObjectType>(new HKObjectType[] { ObjectType }); Tuple <bool, NSError> successError = await _healthStore.RequestAuthorizationToShareAsync(new NSSet(), objectTypesToRead); if (!successError.Item1) { string message = "Failed to request HealthKit authorization: " + (successError.Item2?.ToString() ?? "[no details]"); SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType()); throw new Exception(message); } } }
public override async Task <bool> RequestPermissionAsync(params HealthDataType[] dataTypes) { if (HealthStore == null) { throw new NotSupportedException("HEALTHKIT is not available on this device!"); } var result = dataTypes.Where(IsDataTypeAvailable).ToArray(); if (HKHealthStore.IsHealthDataAvailable && result.Any()) { var(res, error) = await HealthStore.RequestAuthorizationToShareAsync(null, DataTypesToRead(result)).ConfigureAwait(false); if (error != null) { throw new Exception($"HEALTHKIT - Error during Permission request: {error.LocalizedDescription}"); } return(true); } return(false); }
public async Task Connect() { NSSet readTypes = NSSet.MakeNSObjectSet(new HKObjectType[] { StepType, DistanceType, BasalCaloriesType, ActiveCaloriesType, ActiveTimeType }); try { if (HKHealthStore.IsHealthDataAvailable) { Tuple <bool, NSError> success = await _healthStore.RequestAuthorizationToShareAsync(new NSSet(), readTypes); IsConnected = success.Item1; if (IsConnected) { OnConnect?.Invoke(); } else { OnError?.Invoke(success.Item2.Description); } } else { OnError?.Invoke("Is_Health_Data_not_Available".ToUpper()); } } catch (Exception e) { OnError?.Invoke(e.Message); } }
/// <inheritdoc /> public async Task <bool> Enable() { var currentStatus = GetStatus(); if (currentStatus) { return(true); } var accessGranted = await _healthStore.RequestAuthorizationToShareAsync(_dataTypesToWrite, _dataTypesToRead); if (accessGranted.Item1) { var pList = NSUserDefaults.StandardUserDefaults; pList.SetBool(true, _userDefaultsKey); pList.Synchronize(); } else { _log.Warn("Can't obtain authorization to health kit"); } return(accessGranted.Item1); }