domaven / xamarin-plugins

Cross platform Xamarin & Windows plugins for PCLs
MIT License
37 stars 51 forks source link

If CLLocationManager contains CLBeaconRegions, CrossGeofence.Current will throw System.InvalidCastException: Specified cast is not valid. #17

Open kwacks opened 8 years ago

kwacks commented 8 years ago

locationManager.MonitoredRegions includes all CLCircularRegion and CLBeaconRegions which are monitored. If you're monitoring beacons within the app, they will be in the MonitoredRegions collection. Since this component only focusses on geolocations, it makes sense to cast as follows (otherwise System.InvalidCastException: Specified cast is not valid is thrown )

NSSet monitoredRegions = locationManager.MonitoredRegions;

            foreach (var mRegion in monitoredRegions)
            {
                var region = mRegion as CLCircularRegion;
                if (region == null)
                {
                    continue; 
                }
                //If not on regions remove on startup since that region was set not persistent
                if (!Regions.ContainsKey(region.Identifier))
                {
                    locationManager.StopMonitoring(region);
                }
                else
                {
                    locationManager.RequestState(region);
                }

            }

This occurs in the constructor as well as LocationsUpdated().
Hopefully this change can go in for the next update.