繁体   English   中英

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

[英]How Can I Continue updating my location even when app is killed or removed from background in ios?

我的应用程序要求不断更新位置,即使应用程序被杀死或从后台删除也是如此。 它在前景甚至后台模式下都可以正常工作。 我已经尝试了一些代码。

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)
}

希望对您有所帮助。 当您的应用由于某种原因(内存压力)终止时,可能会被唤醒。

但是当应用终止时,后台位置更新存在限制。

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

如果您的应用被用户或系统终止,则当新的位置更新到来时,系统不会自动重新启动您的应用。 用户必须先明确重新启动您的应用,然后才能继续发送位置更新。 自动重新启动应用程序的唯一方法是使用区域监视重大更改位置服务 但是,当用户全局或专门为您的应用禁用“ 后台应用刷新”设置时,系统不会针对任何位置事件(包括重大更改或区域监视事件)重新启动您的应用。 此外,关闭“后台应用程序刷新”后,即使在前台,您的应用程序也不会收到重大更改或区域监视事件。

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

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

这是可能的,但您必须跳过几个步骤。 杀死后发送位置更新的唯一方法是使用区域监视( https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html )。 设置完成后,操作系统将打开您的应用程序,然后您需要花几秒钟的时间来处理信息,然后再终止该应用程序。 它有足够的时间将位置更新发送到服务器/本地存储。 还有另一个名为CLVisit的API,但是它完全由操作系统控制并且不可靠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM