当应用加载时,系统会提示用户启用位置权限。我想只在用户点击"允许"或者"不允许"在那个弹出窗口上。
我看到了一些像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();
}
}
当弹出位置对话框并且状态始终为CLAuthorizationStatus.NotDetermined
时,会提示AuthorizationChanged,并且无法检测用户何时单击"允许"或者"不允许"。
答案 0 :(得分:2)
当应用加载时,系统会提示用户启用位置权限。我想只有当用户在弹出窗口中点击“允许”或“不允许”时才转到下一页。
在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
以将应用程序从先前拒绝更改为允许(始终/ app in-use),也会调用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);
}
}