xamarin iOS:位置请求如何确定用户何时单击“;允许”;或“;Don';不允许”;点击
加载应用程序时,将提示用户启用位置权限。只有当用户在弹出窗口中点击“允许”或“不允许”时,我才想转到下一页 我看到了一些像这样的问题,但它们没有帮助 我的代码:xamarin iOS:位置请求如何确定用户何时单击“;允许”;或“;Don';不允许”;点击,ios,swift,xamarin,xamarin.ios,Ios,Swift,Xamarin,Xamarin.ios,加载应用程序时,将提示用户启用位置权限。只有当用户在弹出窗口中点击“允许”或“不允许”时,我才想转到下一页 我看到了一些像这样的问题,但它们没有帮助 我的代码: var locationManager = new CLLocationManager(); locationManager.AuthorizationChanged += (object sender, CLAuthorizationChangedEventArgs e) => { if(ee.Status == CLA
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();
}
}
当位置对话框弹出且状态始终为CLAuthorizationStatus.NotDetermined
且无法检测用户何时单击“允许”或“不允许”时,将提示AuthorizationChanged
加载应用程序时,将提示用户启用位置权限。只有当用户在弹出窗口中点击“允许”或“不允许”时,我才想转到下一页
在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
将触发
注意:如果指示用户转到设置
将应用程序从先前拒绝更改为允许(始终/应用程序处于使用状态),则还将调用事件处理程序
非常感谢。“这正是我要找的。”开发人员NP很高兴它能帮上忙。
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);
}
}