Ejemplo n.º 1
0
        public CocoaBeaconProvider(CLBeaconRegion beaconRegion)
        {
            Debug.WriteLine("constructor()", LogTag);

            if (!CLLocationManager.LocationServicesEnabled)
            {
                Debug.WriteLine("Location services disabled, my bad.", LogTag);
                return;
            }

            try
            {
                CLBeaconRegion oldRegion = new CLBeaconRegion(beaconRegion.Uuid, beaconRegion.Major.UInt16Value, beaconRegion.Minor.UInt16Value, "TPMS");

                _locationManager.StopMonitoring(oldRegion);
            }
            catch (Exception)
            {
            }

            _locationManagerDelegate = new UniversalBeaconLocationManagerDelegate(this);

            _locationManager          = new CLLocationManager();
            _locationManager.Delegate = _locationManagerDelegate;

            _clBeaconRegion = beaconRegion;
        }
Ejemplo n.º 2
0
        private static async Task <GeolocationAccessStatus> RequestAccessInternalAsync()

        {
            if (CoreDispatcher.Main.HasThreadAccess)
            {
                var mgr = new CLLocationManager();

                lock (_requestManagers)
                {
                    _requestManagers.Add(mgr);
                }

                try
                {
                    var accessStatus = default(GeolocationAccessStatus);
                    var tsc          = new TaskCompletionSource <CLAuthorizationStatus>();

#if __IOS__
                    // Workaround for a bug in Xamarin.iOS https://github.com/unoplatform/uno/issues/4853
                    var @delegate = new CLLocationManagerDelegate();

                    mgr.Delegate = @delegate;

                    @delegate.AuthorizationChanged += (s, e) =>
#else
                    mgr.AuthorizationChanged += (s, e) =>
#endif
                    {
                        if (e.Status != CLAuthorizationStatus.NotDetermined)
                        {
                            tsc.TrySetResult(e.Status);
                        }
                    };

#if __IOS__ //required only for iOS
                    mgr.RequestWhenInUseAuthorization();
#endif

                    if (CLLocationManager.Status != CLAuthorizationStatus.NotDetermined)
                    {
                        accessStatus = TranslateStatus(CLLocationManager.Status);
                    }

                    var cLAuthorizationStatus = await tsc.Task;

                    accessStatus = TranslateStatus(cLAuthorizationStatus);

                    //if geolocation is not well accessible, default geoposition should be recommended
                    if (accessStatus != GeolocationAccessStatus.Allowed)
                    {
                        IsDefaultGeopositionRecommended = true;
                    }

                    return(accessStatus);
                }
                finally
                {
                    lock (_requestManagers)
                    {
                        _requestManagers.Remove(mgr);
                    }
                }
            }
            else
            {
                return(await CoreDispatcher.Main.RunWithResultAsync <GeolocationAccessStatus>(
                           priority : CoreDispatcherPriority.Normal,
                           task : () => RequestAccessInternalAsync()
                           ));
            }
        }