【问题标题】:xamarin iOS: Location Request how to find out when user clicks “Allow” or “Don't allow” is clickedxamarin iOS:位置请求如何找出用户何时单击“允许”或“不允许”被单击
【发布时间】:2017-10-14 00:35:18
【问题描述】:

当应用加载时,系统会提示用户启用位置权限。只有当用户在该弹出窗口上点击“允许”或“不允许”时,我才想移动到下一页。

我看到了一些类似this 的问题,但它们没有帮助。

我的代码:

var locationManager = new CLLocationManager();
locationManager.AuthorizationChanged += (object sender, CLAuthorizationChangedEventArgs e) => 
{
   if(ee.Status == CLAuthorizationStatus.AuthorizedAlways || ee.Status ==CLAuthorizationStatus.Denied)
    {
      //Goto next page
    }
    if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
    {
        locationManager.DesiredAccuracy = CLLocation.AccuracyBest;
        locationManager.DistanceFilter = CLLocationDistance.FilterNone;
        locationManager.RequestAlwaysAuthorization();
    }
}

在弹出位置对话框时提示AuthorizationChanged,状态始终为CLAuthorizationStatus.NotDetermined,无法检测到用户何时点击“允许”或“不允许”。

【问题讨论】:

    标签: ios swift xamarin xamarin.ios


    【解决方案1】:

    当应用加载时,系统会提示用户启用位置权限。只有当用户在该弹出窗口上点击“允许”或“不允许”时,我才想移动到下一页。

    在您的 ViewDidLoad 方法中,您可以执行以下操作:

    // showTrackingMap is a class level var
    showTrackingMap = new LocationCheck((s, ev) =>
    {
        if ((ev as LocationCheck.LocationCheckEventArgs).Allowed)
            Console.WriteLine("Present Tracking Map ViewController");
        else
            Console.WriteLine("Disable Tracking Map");
        showTrackingMap.Dispose();
    });
    

    LocationCheck封装了位置请求,EventHandler将在用户始终接受时触发,仅在应用内或拒绝位置隐私请求。

    注意:如果用户被指示前往 Setting 以将应用程序从以前拒绝更改为允许(始终/应用程序正在使用),也会调用 EventHandler

    public class LocationCheck : NSObject, ICLLocationManagerDelegate
    {
        public class LocationCheckEventArgs : EventArgs
        {
            public readonly bool Allowed;
            public LocationCheckEventArgs(bool Allowed)
            {
                this.Allowed = Allowed;
            }
        }
    
        CLLocationManager locationManager;
        EventHandler locationStatus;
    
        public LocationCheck(EventHandler locationStatus)
        {
            this.locationStatus = locationStatus;
            Initialize();
        }
    
        public LocationCheck(NSObjectFlag x) : base(x) { Initialize(); }
    
        public LocationCheck(IntPtr handle) : base(handle) { Initialize(); }
    
        public LocationCheck(IntPtr handle, bool alloced) : base(handle, alloced) { Initialize(); }
    
        public void Initialize()
        {
            locationManager = new CLLocationManager
            {
                Delegate = this
            };
            locationManager.RequestAlwaysAuthorization();
        }
    
        [Export("locationManager:didChangeAuthorizationStatus:")]
        public void AuthorizationChanged(CLLocationManager manager, CLAuthorizationStatus status)
        {
            switch (status)
            {
                case CLAuthorizationStatus.AuthorizedAlways:
                case CLAuthorizationStatus.AuthorizedWhenInUse:
                    locationStatus.Invoke(locationManager, new LocationCheckEventArgs(true));
                    break;
                case CLAuthorizationStatus.Denied:
                case CLAuthorizationStatus.Restricted:
                    locationStatus.Invoke(locationManager, new LocationCheckEventArgs(false));
                    break;
            }
        }
    
        protected override void Dispose(bool disposing)
        {
            locationStatus = null;
            locationManager.Delegate = null;
            locationManager.Dispose();
            base.Dispose(disposing);
        }
    }
    

    【讨论】:

    • 谢谢。这正是我正在寻找的。​​span>
    • @TheDeveloper NP,很高兴它有帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多