iOS 13后台位置更新权限说明
iOS 13 background location updates permission clarification
我正在努力让我的应用程序通过 iOS 13 上的 'when in use' 权限设置来获取后台位置更新。它在始终允许的情况下工作得很好,但我想要 'When in use'.
有人告诉我,我只需要在信息 plist 中启用后台模式。我启用了这些,下面是我的信息 plist 中的片段。
<key>NSLocationAlwaysUsageDescription</key>
<string>Used for location services</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Need Permission</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Need Permission</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
<string>remote-notification</string>
</array>
简而言之,在 iOS 13 中是否可以通过临时始终权限或使用时权限获取后台位置更新?
编辑:
下面是开始跟踪用户位置的函数:
public static async Task StartJourneyTracking()
{
try
{
InitialiseJourney();
if (Geolocator.IsListening) return;
CLLocationManager manager = new CLLocationManager();
manager.ShowsBackgroundLocationIndicator = true;
Geolocator.DesiredAccuracy =
TrackingConstants.GeoLocatorDesiredAccuracy;
await Geolocator.StartListeningAsync(TimeSpan.FromSeconds(
TrackingConstants.GeoLocatorMinimumTime),
TrackingConstants.GeoLocatorMinimumDistance, false, new
ListenerSettings()
{
ActivityType = ActivityType.AutomotiveNavigation,
AllowBackgroundUpdates = true,
DeferLocationUpdates = false,
PauseLocationUpdatesAutomatically = false
});
IsLocationServiceRunning = true;
Geolocator.PositionChanged += PositionChanged;
Geolocator.PositionError += PositionError;
ShowNotification();
}
catch (Exception e)
{
InstanceManager.LoggingHelper().TrackError(e);
}
}
2019 年 11 月 18 日更新
根据评论中的建议,我发现行为发生了一些变化。
当应用程序在后台最小化时,具有临时始终权限的旅程记录不会获取位置更新。如果我将旅程日志 运行 留在后台(不获取位置更新)并等待第二个提示出现,要求在使用时保留或更改为始终。使用时选择使背景位置指示条立即出现,旅程记录似乎能够在后台获取位置更新。
但是,如果我现在点击后台指示栏将应用程序带到前台并结束旅程记录(停止位置更新)并重新启动它(开始位置更新)而不更改权限,我将不再看到蓝色后台指示条和旅程记录停止在后台接收位置更新。
有什么想法吗?
再次感谢
所以,事实证明地理定位器插件似乎没有正确配置 iOS 13 权限。我为 iOS 的 CLLocationManager 关闭了地理定位器,它第一次工作了。
我正在努力让我的应用程序通过 iOS 13 上的 'when in use' 权限设置来获取后台位置更新。它在始终允许的情况下工作得很好,但我想要 'When in use'.
有人告诉我,我只需要在信息 plist 中启用后台模式。我启用了这些,下面是我的信息 plist 中的片段。
<key>NSLocationAlwaysUsageDescription</key>
<string>Used for location services</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Need Permission</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Need Permission</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
<string>remote-notification</string>
</array>
简而言之,在 iOS 13 中是否可以通过临时始终权限或使用时权限获取后台位置更新?
编辑:
下面是开始跟踪用户位置的函数:
public static async Task StartJourneyTracking()
{
try
{
InitialiseJourney();
if (Geolocator.IsListening) return;
CLLocationManager manager = new CLLocationManager();
manager.ShowsBackgroundLocationIndicator = true;
Geolocator.DesiredAccuracy =
TrackingConstants.GeoLocatorDesiredAccuracy;
await Geolocator.StartListeningAsync(TimeSpan.FromSeconds(
TrackingConstants.GeoLocatorMinimumTime),
TrackingConstants.GeoLocatorMinimumDistance, false, new
ListenerSettings()
{
ActivityType = ActivityType.AutomotiveNavigation,
AllowBackgroundUpdates = true,
DeferLocationUpdates = false,
PauseLocationUpdatesAutomatically = false
});
IsLocationServiceRunning = true;
Geolocator.PositionChanged += PositionChanged;
Geolocator.PositionError += PositionError;
ShowNotification();
}
catch (Exception e)
{
InstanceManager.LoggingHelper().TrackError(e);
}
}
2019 年 11 月 18 日更新
根据评论中的建议,我发现行为发生了一些变化。
当应用程序在后台最小化时,具有临时始终权限的旅程记录不会获取位置更新。如果我将旅程日志 运行 留在后台(不获取位置更新)并等待第二个提示出现,要求在使用时保留或更改为始终。使用时选择使背景位置指示条立即出现,旅程记录似乎能够在后台获取位置更新。
但是,如果我现在点击后台指示栏将应用程序带到前台并结束旅程记录(停止位置更新)并重新启动它(开始位置更新)而不更改权限,我将不再看到蓝色后台指示条和旅程记录停止在后台接收位置更新。
有什么想法吗?
再次感谢
所以,事实证明地理定位器插件似乎没有正确配置 iOS 13 权限。我为 iOS 的 CLLocationManager 关闭了地理定位器,它第一次工作了。