我有一个用Swift开发的iOS应用程序。我的应用程序目前在Swift 2中,但我使用Xcode 8和Swift 3。该应用程序被配置为使用遗留的快速语言版本。
直到最近,应用程序才正常工作。
该应用程序要求始终使用该位置的正确权限,自动化设置为“始终”。
我更新了生产应用程序的签名标识,应用程序停止收到位置更新通知,但仍在开发模式下工作(从xcode启动)。
现在,我撤销并更新了生产和开发证书,而应用程序在后台不更新位置,而自动化设置为始终。
应用程序的安装是正确的,所以我想证书是可以的,但我不明白为什么后台没有更新位置。
我在一个iPhone 7上运行这个应用程序,里面有IOS 10.2和xcode自动管理签名。
以下是我的位置管理器配置:
public class LocationManager : NSObject, ModuleManager, CLLocationManagerDelegate {
/// The core location manager
let coreLocationManager: CLLocationManager
public var datas: JSONable? {
get {
return LocationDatas(locations: self.locations)
}
set {
self.locations = newValue == nil ? [Location]() : newValue as? [Location]
}
}
/// The list of locations to send
private var locations: [Location]?
/// The last location
public var lastLocation: Location? {
return self.locations?.last
}
public override init() {
self.coreLocationManager = CLLocationManager()
if #available(iOS 9.0, *) {
self.coreLocationManager.allowsBackgroundLocationUpdates = true
}
// The accuracy of the location data.
self.coreLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
self.coreLocationManager.distanceFilter = 500; // meters
self.locations = [Location]()
super.init()
self.coreLocationManager.delegate = self
self.locationManager(self.coreLocationManager, didChangeAuthorizationStatus: CLLocationManager.authorizationStatus())
}
// MARK: - CLLocationManager Delegate
public func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
NSLog("location update")
guard locations.count > 0 else {
NSLog("Module Location -- no location available")
return
}
// Add all location waiting in the list to send
self.locations?.appendContentsOf(locations.map { Location(cllocation: $0) })
SDKManager.manager?.sendHeartbeat()
}
public func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch CLLocationManager.authorizationStatus() {
case .NotDetermined:
if #available(iOS 8.0, *) {
self.coreLocationManager.requestAlwaysAuthorization()
} else {
self.coreLocationManager.startUpdatingLocation()
}
case .Denied, .Restricted:
NSLog("Module Location -- access denied to use the location")
case .AuthorizedAlways:
NSLog("AuthorizedAlways")
self.coreLocationManager.startUpdatingLocation()
//self.coreLocationManager.startMonitoringSignificantLocationChanges()
default:
break
}
}
public func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
NSLog("Module Location -- error : \(error)")
}
}
在后台不调用locationManager
函数。
这是我的info.plist:
这是电话授权:
小位置箭头总是在那里,但没有记录位置更新。
发布于 2017-01-04 02:48:17
我检查了您的代码,它似乎很好,如果您已经完成了这些必需的设置,请修改
NSLocationAlwaysUsageDescription
中添加info.plist
如果你没有做第一点,你的应用程序将崩溃,但如果没有做第2点,你的代码将通过,但你永远不会得到更新。
更新:
您的LocationManager
对象似乎是在ARC中发布的。可以尝试通过添加将LocationManager
类更改为Singleton
吗?
static let sharedInstance = LocationManager()
并在代码中访问LocationManager
,如下所示
LocationManager.sharedInstance
发布于 2020-07-14 08:23:20
您不需要只为后台中的位置更新而使用。(可用于其他维护工作,如数据库清理、上传、下载等)
初始化coreLocationManager,时,还可以设置以下属性
// It will allow app running location updates in background state
coreLocationManager.allowsBackgroundLocationUpdates = true
// It will not pause location automatically, you can set it true if you require it.
coreLocationManager.pausesLocationUpdatesAutomatically = false
https://stackoverflow.com/questions/41460503
复制