在iOS 9.3中杀死/终止应用程序后,为什么后台位置不起作用

时间:2022-07-10 01:27:01

Background location not working after kill/ Terminate application. I am using in my application NSLocationAlwaysUsageDescription and Background fetch also on from Capabilities, here my code is :

kill / Terminate应用程序后背景位置不起作用。我在我的应用程序中使用NSLocationAlwaysUsageDescription和后台提取也来自Capabilities,这里我的代码是:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 1000.0
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.startUpdatingLocation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


   func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){

           let currentLocation = manager.location
            let notification = UILocalNotification()
            notification.alertBody = " Lat \(currentLocation?.coordinate.latitude), Long \(currentLocation?.coordinate.longitude)"
            notification.soundName = UILocalNotificationDefaultSoundName
            UIApplication.sharedApplication().scheduleLocalNotification(notification)

            print(" Lat \((currentLocation?.coordinate.latitude))!, Long \((currentLocation?.coordinate.longitude))!")
        }

    }

}

this code is working when click home button but after terminate/kill app in this condition not working please give me the solution here what i missed inside my code.

单击主页按钮时此代码正常工作但在此条件下终止/终止应用程序无法正常工作后,请在此处给我解决方案,我在我的代码中错过了什么。

1 个解决方案

#1


1  

After some changes i got solution my self by doing this changes and code working fine in background mode and foreground mode now able to fetch location.

经过一些更改后,我通过执行此更改获得了解决方案,并且代码在后台模式和前台模式下正常运行,现在可以获取位置。

Swift 3

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = 2000
    if #available(iOS 9.0, *) {
        locationManager.allowsBackgroundLocationUpdates = true
    }
    locationManager.startMonitoringSignificantLocationChanges()
    locationManager.pausesLocationUpdatesAutomatically = true
    locationManager.requestAlwaysAuthorization()

#1


1  

After some changes i got solution my self by doing this changes and code working fine in background mode and foreground mode now able to fetch location.

经过一些更改后,我通过执行此更改获得了解决方案,并且代码在后台模式和前台模式下正常运行,现在可以获取位置。

Swift 3

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = 2000
    if #available(iOS 9.0, *) {
        locationManager.allowsBackgroundLocationUpdates = true
    }
    locationManager.startMonitoringSignificantLocationChanges()
    locationManager.pausesLocationUpdatesAutomatically = true
    locationManager.requestAlwaysAuthorization()