即使应用程序在ios中被杀死或从后台删除,我该如何继续更新我的位置?

时间:2016-09-06 07:29:14

标签: ios swift background location cllocationmanager

我的应用需要不断更新位置,即使应用被杀死或从后台删除。它在前台甚至在后台模式下都能正常工作。但是当应用程序被杀死时它无法工作。 我尝试了一些代码。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.startLocationService()

    if ((launchOptions?[UIApplicationLaunchOptionsLocationKey]) != nil) {
       self.locationmanager = CLLocationManager()
        self.startLocationService()
    }

    print("Location Updates started")


    return true
}

func startLocationService()
{
    self.locationmanager.requestWhenInUseAuthorization()
    self.locationmanager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationmanager.allowsBackgroundLocationUpdates = true
    self.locationmanager.requestAlwaysAuthorization()
    self.locationmanager.delegate = self


    if UIApplication.sharedApplication().backgroundRefreshStatus == .Available {
        print("Background updates are available for the app.")

    }
    else if UIApplication.sharedApplication().backgroundRefreshStatus == .Denied {
        print("The user explicitly disabled background behavior for this app or for the whole system.")

    }
    else if UIApplication.sharedApplication().backgroundRefreshStatus == .Restricted {
        print("Background updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.")

    }
    else
    {
        print("nothing")
    }

    self.locationmanager.startUpdatingLocation()
}



func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
   // locationManager = CLLocationManager()
        }

  func applicationDidBecomeActive(application: UIApplication) {

    self.startLocationService()
}

func applicationWillTerminate(application: UIApplication) {      
             self.locationmanager.startMonitoringSignificantLocationChanges()

}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     print("did update locateion called-->..")
    let location:CLLocation = locations[locations.count-1] 

    print(location.coordinate.longitude)
      print(location.coordinate.latitude)

    let newTime : NSDate = NSDate()

    let calendar: NSCalendar = NSCalendar.currentCalendar()
    let flags = NSCalendarUnit.Second
    let components = calendar.components(flags, fromDate: oldTime, toDate: newTime, options: [])


   //Update locations to server 
    self.call_service_insertLocation("location", loc: location)

}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print(error)
}

2 个答案:

答案 0 :(得分:4)

我希望这对你有所帮助。 当您的应用因某种原因(内存压力)终止时,它可以被唤醒。

但是,当应用终止时,后台更新会受到限制。

这是Apple的位置和地图编程指南中的注释。

  

如果您的应用由用户或系统终止,系统在新的位置更新到达时不会自动重启您的应用。用户必须在恢复位置更新交付之前明确重新启动您的应用。让您的应用自动重新启动的唯一方法是使用区域监控重要更改位置服务。   但是,当用户全局或专门为您的应用停用后台应用刷新设置时,系统不会针对任何位置事件(包括重大更改或区域监控事件)重新启动您的应用。 此外,在关闭后台应用刷新功能时,即使应用程序位于前台,您的应用也不会收到重大更改或区域监控事件。

因此,要在后台接收位置更新,您应该为您的应用启用Background App Refresh设置(您可以在iPhone中的Settings/Your App/Background App Refresh中查看该设置。)

此外,只有在应用程序终止时才会进行重大更改,并且您在此处提到的位置更改(我的意思是kCLLocationAccuracyBest)可能不会唤醒您的应用。 此外,您应该重新启动应用程序委托中的位置管理器 用于检索下一个重要位置更改的application:didFinishLaunching:withOptions方法。还要确保在后台模式下检索位置时尽快返回,或使用后台任务机制使应用程序在后台运行更多时间。 (2~3分钟)。

答案 1 :(得分:0)

它可能,但你必须跳过一些箍。 杀死时发送位置更新的唯一方法是使用区域监控(https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html)。 设置完成后,您的应用程序将由操作系统打开,然后您可以在应用程序被杀之前处理信息。有足够的时间将位置更新发送到您的服务器/本地存储。 还有另一个名为CLVisit的API,但它完全由操作系统控制而且不可靠。