Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
iOS-在应用程序终止时开始监视重大位置更改-不工作_Ios_Swift_Cllocationmanager_Locationmanager - Fatal编程技术网

iOS-在应用程序终止时开始监视重大位置更改-不工作

iOS-在应用程序终止时开始监视重大位置更改-不工作,ios,swift,cllocationmanager,locationmanager,Ios,Swift,Cllocationmanager,Locationmanager,这里有没有人曾经使用过位置服务,即使应用程序没有运行——使用显著的位置变化?基本上,我让应用程序运行,收集位置数据(确认),然后完全关闭应用程序。在下面设置代码 class LocationService: NSObject, CLLocationManagerDelegate { public static let shared = LocationService() var delegate: LocationServiceDelegate? var locatio

这里有没有人曾经使用过位置服务,即使应用程序没有运行——使用显著的位置变化?基本上,我让应用程序运行,收集位置数据(确认),然后完全关闭应用程序。在下面设置代码

class LocationService: NSObject, CLLocationManagerDelegate {

    public static let shared = LocationService()
    var delegate: LocationServiceDelegate?
    var locationManager: CLLocationManager!
    var currentLocation: CLLocation!
    private var startTime: Date?
    private let apiService = APIService()

    private override init() {
        super.init()
        self.setUpLocationServices()
    }

private func setUpLocationServices() {
    self.locationManager = CLLocationManager()
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.allowsBackgroundLocationUpdates = true
    self.locationManager.pausesLocationUpdatesAutomatically = false
    self.locationManager.delegate = self
    self.locationManager.startUpdatingLocation()
    //self.locationManager.startMonitoringSignificantLocationChanges()
}



 // in app delegate

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

        print("starting launching options")

        return true
    }

    func applicationWillTerminate(_ application: UIApplication) {

       self.locationManager.startMonitoringSignificantLocationChanges()
        self.locationManager.stopUpdatingLocation()
        }
据我所知,标准服务和重要服务都可以同时运行(文档称重要的监控将忽略所需的准确性和过滤器)。但是,我确实尝试过使用主AppDelegate中的一些location manager委派方法来分别停止和启动标准服务和重要服务,但它似乎仍然不起作用

我有所有需要的使用说明,并在后台模式功能下检查位置更新。我还确保在收到隐私提示时选择“始终允许”

昨天晚上我开车四处跑了10分钟,远远超过了500米,但应用程序并没有在后台重新启动。我还在模拟器(iOS 12.2)下使用高速公路模式(208)进行了尝试,但仍然不起作用,即使在30分钟后,我也没有得到一个新的位置事件。有没有什么线索可以让它工作,或者我遗漏了什么


我知道过去有一些关于这个的帖子,但大多数都是老帖子。不知道iOS 8/9之后是否发生了变化

非常确定的是,重要的位置更改是基于您预定义(或用户定义)的位置(检测工作的区域)进行的。如果您需要检测“访问”(用户停止行走一段时间的地方),那么您可能需要检查一下。请注意,这些API通常需要在应用程序委托中实例化一个
CLLocationManager
,才能工作。通过在
AppDelegate
类中定义一个属性就足够了:
private let locationManager=CLLocationManager()
。您可以执行基于区域(地理围栏)的操作,但这不是我在这里要做的。另一个选项是开始监视重要的位置更改(如上代码所述)。我不是在跟踪人们步行到某些地方。这将用于骑车/开车的人。无论如何,它应该像访问API一样工作:您需要在AppDelegate中使用
CLLocationManager
的实例。尝试将
let locationManager=CLLocationManager()
locationManager.delegate=self
添加到您的
ApplicationIDFinishLaunchingWithOptions
方法中,它应该可以让您的应用程序重新启动。您可以显示您的
difinishLaunching
代码吗?您需要特别检测您的应用程序是否由位置事件启动,并重新建立位置服务。您不会简单地接收位置委托回调。不,这种方法行不通。一旦应用程序进入后台,就太晚了,无法启动重要的更新。此外,也不能保证
将终止
将被调用。同时运行标准更新和重要位置更改更新应该没有问题。非常确定重要位置更改基于预定义(或用户定义)位置(检测工作的区域)工作。如果您需要检测“访问”(用户停止行走一段时间的地方),那么您可能需要检查一下。请注意,这些API通常需要在应用程序委托中实例化一个
CLLocationManager
,才能工作。通过在
AppDelegate
类中定义一个属性就足够了:
private let locationManager=CLLocationManager()
。您可以执行基于区域(地理围栏)的操作,但这不是我在这里要做的。另一个选项是开始监视重要的位置更改(如上代码所述)。我不是在跟踪人们步行到某些地方。这将用于骑车/开车的人。无论如何,它应该像访问API一样工作:您需要在AppDelegate中使用
CLLocationManager
的实例。尝试将
let locationManager=CLLocationManager()
locationManager.delegate=self
添加到您的
ApplicationIDFinishLaunchingWithOptions
方法中,它应该可以让您的应用程序重新启动。您可以显示您的
difinishLaunching
代码吗?您需要特别检测您的应用程序是否由位置事件启动,并重新建立位置服务。您不会简单地接收位置委托回调。不,这种方法行不通。一旦应用程序进入后台,就太晚了,无法启动重要的更新。此外,也不能保证
将终止
将被调用。同时运行标准更新和重大位置更改更新应该没有问题。