Ios 即使应用程序被终止/终止,位置也会更新
即使在所有州,我都在尝试获取位置更新,即使应用程序被终止/暂停。我已经在xcode中启用了后台提取,并实现了以下代码(使用了引用“”)。但当我终止应用程序时,它在类AppDelegate上给出了一条红线。我不明白这里的问题是什么。我使用了这里的问题“”的解决方案,但它在ios 9中不起作用。请帮助我或告诉我其他解决方案 更新代码-Ios 即使应用程序被终止/终止,位置也会更新,ios,swift,location,core-location,Ios,Swift,Location,Core Location,即使在所有州,我都在尝试获取位置更新,即使应用程序被终止/暂停。我已经在xcode中启用了后台提取,并实现了以下代码(使用了引用“”)。但当我终止应用程序时,它在类AppDelegate上给出了一条红线。我不明白这里的问题是什么。我使用了这里的问题“”的解决方案,但它在ios 9中不起作用。请帮助我或告诉我其他解决方案 更新代码- class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var locationManager: CLLocationManager?
var significatLocationManager : CLLocationManager?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
if(UIApplication.sharedApplication().backgroundRefreshStatus == UIBackgroundRefreshStatus.Available){
print("yessssss")
}else{
print("noooooo")
}
if let launchOpt = launchOptions{
if (launchOpt[UIApplicationLaunchOptionsLocationKey] != nil) {
self.significatLocationManager = CLLocationManager()
self.significatLocationManager?.delegate = self
self.significatLocationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.significatLocationManager!.allowsBackgroundLocationUpdates = true
}
self.significatLocationManager?.startMonitoringSignificantLocationChanges()
}else{
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.locationManager!.allowsBackgroundLocationUpdates = true
}
self.locationManager?.startMonitoringSignificantLocationChanges()
}
}else{
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.locationManager!.allowsBackgroundLocationUpdates = true
}
self.locationManager?.startMonitoringSignificantLocationChanges()
}
return true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let locationArray = locations as NSArray
let locationObj = locationArray.lastObject as! CLLocation
let coord = locationObj.coordinate
}
func applicationDidEnterBackground(application: UIApplication) {
if self.significatLocationManager != nil {
self.significatLocationManager?.startMonitoringSignificantLocationChanges()
}else{
self.locationManager?.startMonitoringSignificantLocationChanges()
}
}
检查本教程:
这里的源代码是:
希望你能得到你的答案。这对我有用。现在,我尝试在调用'didUpdateLocations'函数时发出http请求,以便在应用程序未运行(挂起/终止/终止)时在服务器中存储用户当前位置
我认为苹果不允许在应用程序暂停/终止时发出任何http请求。但若服务器收到位置更新请求,那个么我很乐意接受。因为我不需要服务器的响应。需要大量测试…可能重复:“它在类AppDelegate上给出了一条红线”-这是最糟糕和最没有帮助的错误描述之一。请在这里发布错误消息。如果您通过Xcode运行应用程序,如果您强制终止应用程序,则出现“红线”是正常的。是的,我强制终止应用程序,并显示此红线错误,但我在终止应用程序时未获得位置更新。这里有什么问题???@Cade..此代码不起作用,我正在尝试找到解决方案!!!知道哪一行可能很棒。。。还有错误…嗨,当应用程序处于终止状态时,您是否发现HTTP调用成功?是的。是的。当应用程序状态处于终止状态时。iOS控制应用程序何时唤醒并进行位置更新呼叫。但这对我来说确实有效。根据你的博客,你提到过位置管理器每10分钟更新一次。我知道一个应用程序,即Life 360,一旦用户更改位置,它会立即更新位置。你知道我们如何做到这一点吗?@Leena你可以使用区域监控。@SaqibOmerI我成功地使用location manager在死亡状态下获得了位置更新,它只工作了半个小时,并且在发生重大变化时,我的应用程序正在更新位置。