设定:
我有一个名为LocationUtility
的Swift类,我在ViewController中使用它是这样的:
let locationUtil = LocationUtility()
locationUtil.initLocationManager()
initLocationManager()
函数将CLLocationManager.delegate
设置为我的LocationUtility
类:
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}
永远不会调用以下委托函数:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
我在NSLocationAlwaysUsageDescription
Info.plist
字符串
在我的iPad上,隐私 - >位置服务已启用。
我的LocationUtility类是一个放在一起的东西,主要来自其他stackoverflow问题的代码和有关Swift和iOS 8及更高版本中位置功能的答案。从我的角度来看,我在我的APP和设备上都有正确的设置来接收位置信息。
这是我完整的LocationUtility
类源代码:
import UIKit
import Foundation
import CoreLocation
class LocationUtility: UIViewController, CLLocationManagerDelegate {
var locationStatus : NSString = "Not Started"
var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved : Bool = false
var currentLocation:CLLocation? = nil
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
//locationManager.delegate = self
//locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("LocationUtility -> didFailWithError()")
locationManager.stopUpdatingLocation()
if (seenError == false) {
seenError = true
print(error)
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("LocationUtility -> didUpdateLocations()")
if (locationFixAchieved == false) {
locationFixAchieved = true
//var locationArray = locations as NSArray
currentLocation = locations.last! as CLLocation
let coord = currentLocation!.coordinate
print("user current location")
print(coord.latitude)
print(coord.longitude)
}
}
// authorization status
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("LocationUtility -> didChangeAuthorizationStatus()!")
var shouldIAllow = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access"
shouldIAllow = true
}
if (shouldIAllow == true) {
NSLog("Location to Allowed")
// Start location services
locationManager.startUpdatingLocation()
} else {
NSLog("Denied access: \(locationStatus)")
}
}
}
答案 0 :(得分:1)
您描述的症状强烈建议您的LocationUtility
实例被取消分配,从而取消分配CLLocationManager
并停止整个过程。您不清楚LocationUtility
实例化的位置,但是您需要确定该实例将保留的位置"直播"在CLLocationManager
做其事情的同时在记忆中。例如,如果取消分配视图控制器,那么它的实例变量将被取消分配,看起来它可能包含您的位置管理器。
您的问题最初询问位置管理员是否必须在应用代理中。当然,它并非如此,但确实必须能够阻止在位置更新过程中将其解除分配。
如果您不确定LocationManager
何时被取消分配,请尝试在其上实施deinit
并在该方法上设置断点。