繁体   English   中英

用户在后台时更新位置-迅速

[英]Update location when user is in background - swift

我遇到了3-4个月的问题。 我已经尝试了所有您能想到的方法来使它起作用,但是我真的不能。 现在,我正在寻找您的帮助来解决此问题。

我有一个应用程序,当您按下开始按钮时,它应该可以找到位置。 (当您在应用程序上运行时,效果很好。)但是,一旦离开应用程序,(不终止进程)就会转到后台。 折线的绘制效果不理想。 停顿一下。

我需要一个可以在这里为我提供帮助的人,或者与我创建一个聊天室,以便我们进行讨论,然后我将发送其余的代码。

这是其中的一部分,我认为这是最重要的。 在viewDidLoad内部

 let app = UIApplication.shared

        NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: UIApplication.willResignActiveNotification, object: app)

        NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive(notification:)), name: UIApplication.didBecomeActiveNotification, object: app)

-

 @objc func applicationWillResignActive(notification: NSNotification)
    {
        start = CFAbsoluteTimeGetCurrent()
        print("Background entered")
        startReceivingSignificantLocationChanges()

    }

    @objc func didBecomeActive(notification: NSNotification)
    {
        let elapsed = CFAbsoluteTimeGetCurrent() - start
        counter = counter + Int(elapsed)
        print("Returned to application")
        locationManager.stopMonitoringSignificantLocationChanges()

    }

<在开始按钮内。

//Checking userpermission to allow map and current location
            if (CLLocationManager.locationServicesEnabled())
            {
                locationManager.requestAlwaysAuthorization()
                locationManager.requestWhenInUseAuthorization()

                self.locationManager.allowsBackgroundLocationUpdates = true
                self.locationManager.showsBackgroundLocationIndicator = true

                //Retrieve current position
                if let userLocation = locationManager.location?.coordinate
                {
                    //Zooming in to current position
                    let viewRegion = MKCoordinateRegion(center: userLocation, latitudinalMeters: 200, longitudinalMeters: 200)
                    mapView.setRegion(viewRegion, animated: false)

                    //Creating a start annotation
                    if locations.isEmpty
                    {
                        let annotation = MKPointAnnotation()
                        annotation.title = "Start"
                        annotation.coordinate = userLocation
                        mapView.addAnnotation(annotation)
                    }

                    self.locations.append(userLocation)
                    print(self.locations, "First")

                    //Starts the walk-timer, with interval: 1 second
                    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)

                    //Sending to update
                    update()
                }
            }

<背景工作者

func startReceivingSignificantLocationChanges()
    {
        let authorizationStatus = CLLocationManager.authorizationStatus()
        if authorizationStatus != .authorizedAlways
        {
            return
        }

        if !CLLocationManager.significantLocationChangeMonitoringAvailable()
        {
            // The service is not available.
            return
        }
        else
        {
            locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
            locationManager.distanceFilter = 100.0 //100.0 meters
            locationManager.activityType = .fitness
            locationManager.allowsBackgroundLocationUpdates = true
            locationManager.delegate = self
            locationManager.startMonitoringSignificantLocationChanges()
        }
    }

    func locationManager(_ manager: CLLocationManager,  didUpdateLocations
        locations: [CLLocation])
    {
        let lastLocation = locations.last!

        self.locations.append(lastLocation.coordinate)

        print("Locations retrieved from background: ", self.locations)
    }

我还有很多要给你看的。 但是不幸的是,这太多了……

请从项目的功能启用后台模式,然后启用“位置更新”。 启用此功能后,唯一在后台获取更新(不处于终止状态)的配置是将“ allowsBackgroundLocationUpdates”设置为true(您已经完成)。

在这里,仅当您希望在用户杀死应用程序时获取位置时,才需要进行重大位置更改。 位置的重大更改将在后台启动应用程序并读取设备的位置。 有关在后台获取位置的更多信息,请遵循:

https://developer.apple.com/documentation/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates

要在应用程序处于终止状态时进行重要的位置更改,请点击以下链接。 这是在目标C中完成的,但也可以轻松快速地完成。

http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

希望这可以帮助。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM