Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么在iOS 9.3中杀死/终止应用程序后后台位置不起作用_Iphone_Swift_Swift2_Core Location_Ios9.3 - Fatal编程技术网

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

Iphone 为什么在iOS 9.3中杀死/终止应用程序后后台位置不起作用,iphone,swift,swift2,core-location,ios9.3,Iphone,Swift,Swift2,Core Location,Ios9.3,终止/终止应用程序后后台位置不工作。我正在我的应用程序中使用NSLocationAlwaysUsageDescription和后台获取功能,我的代码如下: import UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate{ var locationManager: CLLocationManager! override func viewDid

终止/终止应用程序后后台位置不工作。我正在我的应用程序中使用NSLocationAlwaysUsageDescription和后台获取功能,我的代码如下:

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))!")
        }

    }

}

此代码在单击“主页”按钮时起作用,但在此情况下终止/杀死应用程序后不起作用,请在此处为我提供解决方案,我在代码中遗漏了什么

在做了一些更改之后,我自己得到了解决方案,通过做这些更改,代码在后台模式和前台模式下都能正常工作,现在可以获取位置信息了

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()

你好Rob现在我得到了解决方案,当应用程序现在处于kill/terminate状态时,我的代码工作正常,并且能够调用web服务和位置。更新的代码在terminated状态下不工作。使用Xcode 8和iOS 9.3.5设备。仅在视图控制器中写入所有代码。我需要在应用程序代理上写的任何内容。请帮帮我…你知道你的更新代码起作用的原因吗??如果没有,请阅读苹果文档。无需同时使用startUpdatingLocation()和StartMonitoringSignificationLocationChanges()。您只需要使用startMonitoringSignificantLocationChanges()来完成任务。如果有任何疑问,您可以询问我。上述代码在终止状态下不工作。你能帮我吗?Xcode 8和9.3.5设备是的,我将在一段时间后更新Swift 3的上述代码。我只在视图控制器中编写了所有代码。我需要在appdelegate.swift上写入的任何内容,以便在终止状态下进行位置跟踪。请帮助我。您可以将此代码与Singleton类的使用以及上面的代码一起使用,因为它也是有效的,因为当您进行距离覆盖时,委托方法将始终调用。每次委托都会在后台和前台给出值,但您需要在所有条件下对其进行管理。我已经完成了这项工作,并且在iOS 10中也工作得很好。