protected override IEnumerable <Datum> Poll(CancellationToken cancellationToken) { List <Datum> data = new List <Datum>(); NSError error; NSDate dateOfBirth = HealthStore.GetDateOfBirth(out error); if (error == null) { if (dateOfBirth == null) { throw new Exception("User has not provided -- or has not allowed access to -- their date of birth."); } else { data.Add(new BirthdateDatum(DateTimeOffset.Now, new DateTimeOffset(dateOfBirth.ToDateTime()))); } } else { throw new Exception("Error reading date of birth: " + error.Description); } return(data); }
void UpdateUsersAge() { NSError error; NSDate dateOfBirth = HealthStore.GetDateOfBirth(out error); if (error != null) { Console.WriteLine("An error occured fetching the user's age information. " + "In your app, try to handle this gracefully. The error was: {0}", error); return; } if (dateOfBirth == null) { return; } var now = NSDate.Now; NSDateComponents ageComponents = NSCalendar.CurrentCalendar.Components(NSCalendarUnit.Year, dateOfBirth, now, NSCalendarOptions.WrapCalendarComponents); nint usersAge = ageComponents.Year; ageHeightValueLabel.Text = string.Format("{0} years", usersAge); }