Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 应用程序终止时,位置后台模式是否在swift模式下工作?_Ios_Swift_Push Notification_Cllocationmanager - Fatal编程技术网

Ios 应用程序终止时,位置后台模式是否在swift模式下工作?

Ios 应用程序终止时,位置后台模式是否在swift模式下工作?,ios,swift,push-notification,cllocationmanager,Ios,Swift,Push Notification,Cllocationmanager,现在,我正在尝试为我的应用程序实现位置背景模式。当用户位置被更新时,我将安排一个通知,最终我将有条件地安排它们,但现在我只是安排它们,以便我知道它是有效的。我有以下代码: func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.first { currentLocation =

现在,我正在尝试为我的应用程序实现位置背景模式。当用户位置被更新时,我将安排一个通知,最终我将有条件地安排它们,但现在我只是安排它们,以便我知道它是有效的。我有以下代码:

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.first {
        currentLocation = location
    }
    if UIApplication.shared.applicationState == .background {
    // if the app is in the background
         let notif = UNMutableNotificationContent()
    notif.title = "Location was updated"
    var message = " "
    notif.body = message
    let identifier = String(arc4random())
    var dc = DateComponents()
    var date = Date()
    date = Calendar.current.date(byAdding: .second, value: 3, to: date)!
    dc.hour = Calendar.current.component(.hour, from: date)
    dc.minute = Calendar.current.component(.minute, from: date)
    dc.second = Calendar.current.component(.second, from: date)
    let notifTrigger = UNCalendarNotificationTrigger(dateMatching: dc, repeats: false)
    let request = UNNotificationRequest(identifier: identifier, content: notif, trigger: notifTrigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
        if error != nil {
            print(error!)
        } else {
            print("Notification scheduled!")
        }
    })
当应用程序在后台时,这非常有效,但是当应用程序终止时,我的函数不会被调用,通知也不会被传递。我的问题是:斯威夫特能做到这一点吗?或者,我正在使用的locationManager功能是否仅在应用程序处于后台但未终止时才起作用


旁注:我尝试过去掉if UIApplication.shared.applicationState==.background,但它无法解决可能出现的问题。移动应用程序,以便触发位置更新。检查精度为100米,移动设备或使用模拟器模拟定位。我假设您已经授予使用位置始终权限

当您终止应用程序时,正常的*位置服务将停止,直到您从非后台状态运行startUpdatingLocation

*访问位置服务、区域监控、重大位置更改不是正常的位置跟踪,而是在操作系统级别而非应用程序级别进行管理


非正常位置跟踪在应用程序终止后不会停止

您如何在非后台状态下运行startUpdatingLocation,即当应用程序终止时?什么是非后台状态?您认为您是如何达到这种状态的?例如,当应用程序终止时,当用户完全关闭应用程序时,您只能在应用程序处于前台时才能这样做。或者是在后台启动的,例如通过进入地理围栏,或者获得SLC回调如果我理解正确,如果应用程序终止且位置更改,将不会调用didUpdateLocations?只有当应用程序打开时才会调用它?是的,我有。我已经让模拟器做了高速驱动,如果应用程序是后台的,但没有终止,那么它就可以工作,但是当它终止时,通知就会停止发送。对于模拟器,调试器会断开连接。您尝试过该设备吗?由于调试器被终止,高速驱动器也被终止。在同一模拟器上运行另一个应用程序,并从该应用程序模拟位置。