ios - 使用位置管理器时,我的 Swift 应用程序耗电非常快

标签 ios swift core-location locationmanager

在我的 Swift 应用程序中,我正在检查用户的位置并将其发送到我的后端代码。

我的代码都放在 AppDelegate 中,看起来像这样:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    NotificationCenter.default.addObserver(self, selector:
        #selector(AppDelegate.notificationConfirmationSent),
        name: NSNotification.Name(rawValue: locationUpdKey), object: nil)

   initLocationManager()

   return true
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    location = locations.last
    let coord = location.coordinate
    longitude = coord.longitude
    latitude = coord.latitude
    NotificationCenter.default.post(name: Notification.Name(rawValue: locationUpdKey), object: self)
    location_fixed = true;
}


func notificationConfirmationSent()
{

    if(defaults.object(forKey: "uniqueId") != nil) {
        let currentTime = Date().timeIntervalSince1970
        if (currentTime - timeInterval > 30) {
            print("SENDING DATA TO WEBSERVICE FROM NOTIFICATION")
            timeInterval = currentTime
            sendCurrentUserPositionToWebService(self.longitude, latitude: self.latitude, uniqueId: defaults.string(forKey: "uniqueId")!)
        }
    }
}

除此之外,我还使用了这两种方法:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .notDetermined:
        print("NotDetermined")
    case .restricted:
        print("Restricted")
    case .denied:
        print("Denied")

    case .authorizedAlways:
        print("AuthorizedAlways")
    case .authorizedWhenInUse:
        print("AuthorizedWhenInUse")
        locationManager.distanceFilter = 200
        locationManager.startUpdatingLocation()
    }
}

func initLocationManager() {
    print("init location manager app delegate")
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
        //locationManager.startMonitoringSignificantLocationChanges()

        locationManager.distanceFilter = 200

        locationManager.startUpdatingLocation()
    } else {

        locationManager.requestWhenInUseAuthorization()
    }

}

现在,在使用我的应用程序一段时间后,电池消耗得非常快,而且整个手机都在变热。我想更改此行为并修复它,因此手机只发送一次位置(当它是 fixed 时),然后在位置发生重大变化时发送。 您能否提示我如何从这里开始以及为什么电池耗电如此之快?

最佳答案

就电池消耗而言,运行 Instruments 并查看消耗 CPU 的是什么。我的猜测是它不是位置管理器。

就只知道重大变化而言,Apple 有一个 API。

startMonitoringSignificantLocationChanges()

Note Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

关于ios - 使用位置管理器时,我的 Swift 应用程序耗电非常快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42632381/

相关文章:

swift - 如何在 Swift viewDidLoad 中获取邮政编码?

ios - 初学者在iOS中使用assimp

ios - 从 iPhone 本地存储读取文件

ios - CreateML图像分类器背景信息

ios - swift 3 : Dismiss popup ViewController

ios - Swift 2 从字符串转换为数字

ios - 在 swift : Difference between Array VS NSArray VS [AnyObject]

ios - 为什么 locationManager didUpdateLocation 不工作?

iphone - 单击 "OK"后首次运行时 GPS 未启动

ios - 在 Cargo Build 中为 iOS 目标启用位码输出?