【问题标题】:Why background location is not working after kill/Terminate app in iOS 9.3为什么在 iOS 9.3 中杀死/终止应用程序后后台位置不起作用
【发布时间】:2016-07-11 04:57:31
【问题描述】:

杀死/终止应用程序后后台位置不起作用。我在我的应用程序中使用 NSLocationAlwaysUsageDescription 和 Background fetch 也来自 Capabilities,这里我的代码是:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 1000.0
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.startUpdatingLocation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


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

        if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){

           let currentLocation = manager.location
            let notification = UILocalNotification()
            notification.alertBody = " Lat \(currentLocation?.coordinate.latitude), Long \(currentLocation?.coordinate.longitude)"
            notification.soundName = UILocalNotificationDefaultSoundName
            UIApplication.sharedApplication().scheduleLocalNotification(notification)

            print(" Lat \((currentLocation?.coordinate.latitude))!, Long \((currentLocation?.coordinate.longitude))!")
        }

    }

}

此代码在单击主页按钮时有效,但在此情况下终止/终止应用程序后无法正常工作,请在此处给出我在代码中遗漏的解决方案。

【问题讨论】:

  • 你好 Rob 现在我得到了解决方案,当应用程序处于终止/终止状态并且还能够调用 Web 服务和位置时,我的代码工作正常。
  • 更新的代码在终止状态下不起作用。使用 Xcode 8 和 iOS 9.3.5 设备。仅在视图控制器中编写了所有代码。我需要在应用程序委托上写的任何内容。请帮帮我。

标签: iphone swift swift2 core-location ios9.3


【解决方案1】:

经过一些更改后,我通过执行这些更改和代码在后台模式和前台模式下正常工作,现在能够获取位置来解决我自己的问题。

斯威夫特 3

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = 2000
    if #available(iOS 9.0, *) {
        locationManager.allowsBackgroundLocationUpdates = true
    }
    locationManager.startMonitoringSignificantLocationChanges()
    locationManager.pausesLocationUpdatesAutomatically = true
    locationManager.requestAlwaysAuthorization()

【讨论】:

  • ..你知道你的更新代码工作的原因吗?如果没有,请阅读 Apple 文档。无需同时使用 startUpdatingLocation() 和 startMonitoringSignificantLocationChanges()。您只需要使用 startMonitoringSignificantLocationChanges() 即可完成任务。如有任何疑问,可以问我。
  • 以上代码在终止状态下不工作。你可以帮帮我吗? Xcode 8 和 9.3.5 设备
  • 是的,我会在一段时间后为 Swift 3 更新上面的代码。
  • 仅在视图控制器中编写了所有代码。我需要在 appdelegate.swift 上写的任何内容,以便在终止状态下进行位置跟踪。请帮帮我。
  • 您可以通过使用 Singleton 类和上述代码两种方式使用此代码,因为它也可以工作,因为当您距离覆盖时,委托方法将始终调用。每次委托都会在后台和前台提供价值,但您需要在所有条件下对其进行管理。我已经完成了这件事,并且在 iOS 10 中也运行良好。
猜你喜欢
  • 2016-11-08
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
相关资源
最近更新 更多