Ios 获取所有应用程序状态下的用户当前位置(lat/long),而不仅仅是在需要我的应用程序时,无位置管理器委托
我正在开发watch heart beat应用程序,我希望当用户心率达到临界值时,我们将获取他的当前位置(前台、后台和终端),并将其发送到我们的服务器。是否有任何方法可以让我只在那个位置获取用户位置。我不想每次都更新他的位置。要获取用户位置,您必须声明:Ios 获取所有应用程序状态下的用户当前位置(lat/long),而不仅仅是在需要我的应用程序时,无位置管理器委托,ios,swift,location,health-monitoring,Ios,Swift,Location,Health Monitoring,我正在开发watch heart beat应用程序,我希望当用户心率达到临界值时,我们将获取他的当前位置(前台、后台和终端),并将其发送到我们的服务器。是否有任何方法可以让我只在那个位置获取用户位置。我不想每次都更新他的位置。要获取用户位置,您必须声明: let locationManager = CLLocationManager() 在你的控制器里 然后,在viewdiload中,您必须请求位置并初始化CLLocationManagerget过程: // Ask for Authorisa
let locationManager = CLLocationManager()
在你的控制器里
然后,在viewdiload
中,您必须请求位置并初始化CLLocationManager
get过程:
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
您将在CLLocationManagerDelegate
中获得位置:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var location:CLLocationCoordinate2D = manager.location.coordinate
print("locations = \(location.latitude) \(location.longitude)")
}
在您的info.plist
中,您必须添加NSLocationAlwaysUsageDescription
和自定义警报消息,以便在请求位置时显示
干杯…但是当我的应用程序处于终止状态时,我如何获取位置如果用户终止应用程序,系统将不再授予它相同的权限。后台抓取操作和后台位置将不会执行,直到用户决定再次启动你的应用程序。请阅读我的描述,我已经提到了它。我知道我们可以通过重要的位置更新获得当前位置,但它的非更新仍在继续