简体   繁体   English

即使应用程序在后台运行,如何使用swift在ios中获取位置更新

[英]How to get location update in ios using swift even when the app is running in background

I am developing an app in which i need to update user's location regularly, but i am not able to get location update when the app is suspended or running in background. 我正在开发一个应用程序,我需要定期更新用户的位置,但是当应用程序暂停或在后台运行时,我无法获得位置更新。 I have also enabled background location update in capabilities. 我还在功能中启用了后台位置更新。

viewDidLoad code: viewDidLoad代码:

override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        let user=FIRAuth.auth()?.currentUser?.displayName
        if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse){
            locationManager.requestLocation()
            location=locationManager.location
            locationManager.startUpdatingLocation()
            locationManager.startMonitoringSignificantLocationChanges()
            //updating to database
            ref.child("\(user!)/lat").setValue(location.coordinate.latitude)
            ref.child("\(user!)/long").setValue(location.coordinate.longitude)
            //location=locationManager.location
        }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location=locations[locations.count-1]
    }

Enable location update -> Background Mode -> Capabilities 启用位置更新 - >背景模式 - >功能

I was using this method get location in background. 我正在使用这种方法在后台获取位置。 This method write in AppDelegate class 此方法在AppDelegate类中编写

 func getLocation() {
    self.locationManager = CLLocationManager()
    self.locationManager.delegate = self
    if self.locationManager.responds(to: #selector(self.requestAlwaysAuthorization)) {
        self.locationManager.requestAlwaysAuthorization()
    }
    self.locationManager.distanceFilter = kCLDistanceFilterNone
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    if !CLLocationManager.locationServicesEnabled() {
            // location services is disabled, alert user
        var servicesDisabledAlert = UIAlertView(title: "Alert", message: "Location service is disable. Please enable to access your current location.", delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "")
        servicesDisabledAlert.show()
    }
    else {
        self.locationManager.startUpdatingLocation()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使应用未在前台运行,我们也可以定期获取IOS中的位置更新吗? - can we get location updates in IOS swift in a regular intervel of time even when app is not running in foreground IOS - 使用swift运行更新用户位置的后台任务 - IOS - Running background task for update user location using swift 即使应用程序未在 Swift 中运行,也能接收位置信息 - Receiving Location even when app is not running in Swift Swift 3-即使应用程序在后台运行功能 - Swift 3 - Running a function even when app is in background 即使用户覆盖50米,当应用程序未在ios10的后台运行(暂停)时,如何每隔n秒获取当前的GPS位置? - How can I get current GPS location in every n seconds when app is not running in background (suspended) for ios10 even if user covers 50 meters? 即使应用程序在后台运行,如何使用通知信息更新TableView? - How to update a TableView with the notification information even when the app is running in the background? 如何在iOS应用程序中在线更新后台位置? - How to update location in background online in an iOS app? iOS位置更新甚至应用程序都没有运行(比如找我的iphone) - iOS Location update even app is not running (like find my iphone) 当应用程序在后台甚至被用户终止时,获取用户位置而无需在iOS上移动 - Get user location without movement on iOS when app is in background or even terminated by user 获取iOS应用在后台甚至被杀的位置 - Getting location for an iOS app when it is in the background and even killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM