用户在后台时更新位置 - 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(您已经完成了(。

在这里,仅当您要在用户杀死应用程序时要获得位置时,才需要重大更改。这种重要的位置更改将在后台启动应用程序,并阅读设备的位置。有关获取背景位置的更多信息,请如下:

对于申请处于杀戮状态时的重大位置更改,请在链接下面遵循。这是在目标C中,但也可以在Swift中很容易完成。

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

希望这会有所帮助。

最新更新