繁体   English   中英

应用程序被杀死后,如何找到用户的位置?

[英]how can i find users location when app is killed ?

应用运行时,我可以找到用户的当前位置。 但是当应用被杀死时,我找不到位置。 我如何找到位置?

这段代码给我一个当前的位置;

 func determineMyCurrentLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
        locationManager.startUpdatingHeading()

    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation:CLLocation = locations[0] as CLLocation


    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")
    latitude = userLocation.coordinate.latitude
    longitude = userLocation.coordinate.longitude
    UserDefaults(suiteName: "group.com.ReelKapi")!.set(latitude, forKey:"latitude")
    UserDefaults(suiteName: "group.com.ReelKapi")!.set(longitude, forKey:"longitude")


}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
    print("Error \(error)")
}

强烈推荐阅读

首先,应用程序的用户必须允许您的应用程序在后台监视位置。 我看到您正在使用requestAlwaysAuthorization()请求该权限,因此该部分已涵盖。

另外,您必须将allowBackgroundLocationUpdates设置为YES。

接下来,您应该在应用程序包的plist中添加特定的“ 后台模式” location

完成所有这些设置后,在BG模式下,您的应用将能够从您的代码正在使用的标准位置服务中获取事件( startUpdatingLocation() )。

如果您的应用被用户或iOS杀死,它将再次加载到后台以接收区域进入/退出事件,访问事件或重要的位置更新。 因此,如果您希望重新启动应用程序,则必须使用其中之一。

当应用程序被杀死时,操作系统不允许运行任何代码,因此您无法获得任何更新,尽管您可以在nsuserDefaults中使用以前保存的位置。 仅当用户重新启动您的应用程序进入前台状态时,您才能从位置管理器获取更新位置。

暂无
暂无

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

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