gpt4 book ai didi

iOS - 如何将 allowedBackgroundLocationUpdates 设置为 YES?

转载 作者:行者123 更新时间:2023-11-30 11:45:38 24 4
gpt4 key购买 nike

也许这是一个愚蠢的问题,但我真的无法在任何地方找到答案。我想让我的 iOS 应用程序在后台时将位置更新发送到网络服务器。目前它不起作用,我知道原因是因为 allowedBackgroundLocationUpdates 默认设置为 NO。我的问题是我可以/应该在哪里将 allowedBackgroundLocationUpdates 设置为 YES?

编辑:让我补充一下,我已经修改了 Info.plist 以包括:所需的后台模式 - 应用程序注册位置更新

最佳答案

希望我理解你的问题,首先需要获得用户的权限,调用requestAlwaysAuthorization()方法

Apple 的代码示例:

let locationManager = CLLocationManager()   
func enableLocationServices() {
locationManager.delegate = self

switch CLLocationManager.authorizationStatus() {
case .notDetermined:
// Request when-in-use authorization initially
locationManager.requestAlwaysAuthorization()
break

case .restricted, .denied:
// Disable location features
disableMyLocationBasedFeatures()
break

case .authorizedAlways:
// Enable any of your app's location features
enableMyAlwaysFeatures()
break
}
}
}

注意:- 您需要在应用的 Info.plist 文件中包含 NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysAndWhenInUseUsageDescription 键。 (如果您的应用支持 iOS 10 及更早版本,则还需要 NSLocationAlwaysUsageDescription 键。)如果这些键不存在,授权请求会立即失败。

验证这些步骤:
1.设置位置管理器的allowsBackgroundLocationUpdates为YES。
2.调用requestAlwaysAuthorization()方法
3.在info.plist中添加NSLocationAlwaysUsageDescription。
4.配置应用程序在后台运行(plist 中的 UIBackgroundModes),可能在沙箱中运行。

关于iOS - 如何将 allowedBackgroundLocationUpdates 设置为 YES?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48854671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com