private void UpdateWorkoutLocationsRoute(HKWorkout workout, HKWorkoutRoute route, List <CLLocation> newLocations) { // create a workout route builder var workoutRouteBuilder = new HKWorkoutRouteBuilder(this.healthStore, null); // insert updated route locations workoutRouteBuilder.InsertRouteData(newLocations.ToArray(), (success, error) => { if (success) { var syncIdentifier = route.Metadata?.SyncIdentifier; if (!string.IsNullOrEmpty(syncIdentifier)) { // new metadata with the same sync identifier and a higher version var objects = new NSObject[] { new NSString(syncIdentifier), NSNumber.FromInt32(2) }; var keys = new NSString[] { HKMetadataKey.SyncIdentifier, HKMetadataKey.SyncVersion }; var dictionary = NSDictionary.FromObjectsAndKeys(objects, keys); var metadata = new HKMetadata(dictionary); // finish the route with updated metadata workoutRouteBuilder.FinishRoute(workout, metadata, (workoutRoute, routeRrror) => { if (workoutRoute != null) { Console.WriteLine($"Workout route updated: ({route})"); } else { Console.WriteLine($"An error occurred while finishing the new route: ({error?.LocalizedDescription ?? "Unknown"})"); } }); } else { throw new ArgumentNullException(nameof(syncIdentifier), "Missing expected sync identifier on route"); } } else { Console.WriteLine($"An error occurred while inserting route data ({error?.LocalizedDescription ?? "Unknown"})"); } }); }
public void StartAccumulatingLocationData() { if (CLLocationManager.LocationServicesEnabled) { this.locationManager = new CLLocationManager { Delegate = this, AllowsBackgroundLocationUpdates = true, DesiredAccuracy = CLLocation.AccuracyBest, }; this.locationManager.StartUpdatingLocation(); this.workoutRouteBuilder = new HKWorkoutRouteBuilder(this.healthStore, null); } else { Console.WriteLine("User does not have location service enabled"); } }
protected override void Dispose(bool disposing) { base.Dispose(disposing); this.healthStore.Dispose(); this.workoutEvents.Clear(); this.activeDataQueries.Clear(); if (this.locationManager != null) { this.locationManager.Dispose(); this.locationManager = null; } if (this.workoutRouteBuilder != null) { this.workoutRouteBuilder.Dispose(); this.workoutRouteBuilder = null; } }