首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XCode 11:后台监控不起作用

XCode 11:后台监控不起作用
EN

Stack Overflow用户
提问于 2020-06-17 15:11:07
回答 1查看 30关注 0票数 0

经过几个小时的阅读和重新编码,我仍然找不到解决方案。

我想建立一个简单的应用程序,跟踪我的位置在后台。在第一次加载应用程序时,我在当前位置周围设置了一个区域,开始监控这个区域和stopUpdatingLocations。一旦应用程序获得didExitRegion事件,我再次获得当前位置,设置另一个区域(在清理所有其他受监控区域之后),然后所有操作再次启动。

这个应用有定位的alwaysAuthorization,设置了位置更新的后台模式,PList包含LocationAlwaysAndWhenInUseUsageDescription和LocationWhenInUseUsageDescription。

有一段时间,它工作得很好。但在长时间不移动之后,应用程序被挂起,不再接收任何位置更新,甚至不会收到监视器事件,这应该会在我正确理解this ()时重新启动应用程序。

有谁能给我点提示吗?

谢谢。

代码语言:javascript
复制
import UIKit
import CoreLocation
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

let locationManager = CLLocationManager()

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


    let currentNotifications = UNUserNotificationCenter.current()


    currentNotifications.getNotificationSettings { (settings) in
        if settings.authorizationStatus == .notDetermined {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in }
        }
    }


    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.startUpdatingLocation()


    return true
}

}

extension AppDelegate: CLLocationManagerDelegate {


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


    let location = locations[0]


    let now = Date()
    let formatter = DateFormatter()


    formatter.timeZone = TimeZone.current
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"


    for region in locationManager.monitoredRegions {
        locationManager.stopMonitoring(for: region)
    }


    var regionIdentifier = String(format: "%f", location.coordinate.latitude) + ", " + String(format: "%f", location.coordinate.latitude) + "( set on " + formatter.string(from: now) + ")"


    var geofenceRegion = CLCircularRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), radius: 250, identifier: regionIdentifier)


    geofenceRegion.notifyOnExit = true
    locationManager.startMonitoring(for: geofenceRegion)


    locationManager.stopUpdatingLocation()


}




func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    locationManager.stopMonitoring(for: region)
    sendNotification(title: "Event triggered", subtitle: "Region exit", body: region.identifier)
    locationManager.startUpdatingLocation()
}


}

public func sendNotification(title: String, subtitle: String, body: String) {


let content = UNMutableNotificationContent()
content.title = title
content.subtitle = subtitle
content.body = body
content.sound = .default


let identifier = "geoStalker4_notification_" + String(format: "%f", NSDate().timeIntervalSince1970)


let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)


let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)


UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)


}
EN

回答 1

Stack Overflow用户

发布于 2020-11-23 21:10:56

尝试将此内容添加到您的locationManager设置:

代码语言:javascript
复制
locationManager.pausesLocationUpdatesAutomatically = false
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62423356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档