【发布时间】: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