使用iOS App,Swift获取设备/用户位置的问题

时间:2016-04-16 19:07:06

标签: ios swift core-location

设定:

  1. Xcode 7.3(7D175)
  2. Swift 2
  3. Device是iOS 9.3.1的iPad
  4. 我有一个名为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)")
                }
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

您描述的症状强烈建议您的LocationUtility实例被取消分配,从而取消分配CLLocationManager并停止整个过程。您不清楚LocationUtility实例化的位置,但是您需要确定该实例将保留的位置"直播"在CLLocationManager做其事情的同时在记忆中。例如,如果取消分配视图控制器,那么它的实例变量将被取消分配,看起来它可能包含您的位置管理器。

您的问题最初询问位置管理员是否必须在应用代理中。当然,它并非如此,但确实必须能够阻止在位置更新过程中将其解除分配。

如果您不确定LocationManager何时被取消分配,请尝试在其上实施deinit并在该方法上设置断点。