iPhone:捕获应用程序在后台运行时的事件

发布于 2024-12-27 23:55:38 字数 658 浏览 1 评论 0原文

谁能告诉我当我的应用程序在后台运行时是否可以处理诸如 OrientationChanged、Shake、LocationChanged 等事件?

我尝试过以下代码,但只有当我的应用程序在前台运行时才会调用它!

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session
if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 120) {     
    self.currentLocation = newLocation;
}
NSLog(@"lat: %f long:%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
}

谢谢。

Can anyone please let me know if it is possible to handle events like OrientationChanged, Shake, LocationChanged etc.. when my App running in background?

I have tried following code but it gets invoked only when my app running in foreground!

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session
if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 120) {     
    self.currentLocation = newLocation;
}
NSLog(@"lat: %f long:%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2025-01-03 23:55:38

http://developer.apple.com/ library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

您应该在您的info.plist 文件位于 UIRequiredDeviceCapability 键下

,那么您的应用将在后台接收位置更新。
然而,这会以更快的速度耗尽电池..无需用户了解太多..如果可能的话,您应该仅注册重要位置

方向更改的更新将无法在后台进行。
摇晃可能是……我不确定

http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

You should include location-services in your info.plist file under the key UIRequiredDeviceCapabilities

then your app will receive location updates in the background.
However this drains the battery at a faster rate..without user knowing much..If possible you should register for only significant location

orientation changed updates won't be possible in background.
shake might be..i am not sure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文