2017-10-14 50 views
1

当应用程序加载时,会提示用户启用位置权限。只有当用户在该弹出窗口中点击“允许”或“不允许”时,我才想移至下一页。xamarin iOS:位置请求如何找出用户单击“允许”或“不允许”时的点击次数

我看到一些问题,如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,无法当用户点击“允许”或“不允许”来检测。

回答

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将触发。

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

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); 
    } 
} 
+0

谢谢。这正是我正在寻找的。 – TheDeveloper

+0

@TheDeveloper NP,很高兴它有帮助。 – SushiHangover

相关问题