即使应用程序在 ios 中被杀死或从后台删除,我如何继续更新我的位置? [英] How Can I Continue updating my location even when app is killed or removed from background in ios?

查看:20
本文介绍了即使应用程序在 ios 中被杀死或从后台删除,我如何继续更新我的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My app requires to update location continuously, even if app is killed or removed from background. It works fine in foreground and even in background mode.but not working when app is killed. I've tried some code.

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

推荐答案

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

Its possible, but you'll have to jump through a few hoops. The only way to send location updates when killed is by using Region Monitoring (https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html). When setup up, your app would be opened by the OS then you have a few seconds to process information before the app is killed. Its enough time to send location updates to your server/local storage. There is also another API called CLVisit, however, its completely controlled by the operating system and not reliable.

这篇关于即使应用程序在 ios 中被杀死或从后台删除,我如何继续更新我的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆