设计 iPhone iOS4 应用程序行为:后台运行与终止应用程序
我们正在为汽车驾驶员开发 iPhone GPS 应用程序。
您可能知道,iOS4 引入了多任务处理,因此我们的应用程序可以在后台运行 - 确实如此。这是其功能的一部分。
问题在于 iOS4 上关闭应用程序的标准方法。以下是两种情况:
1) 用户希望将应用程序置于后台:
通常,在 iPhone iOS4 上,只需按“主页”按钮即可。
2) 用户想要关闭应用程序(即到达目的地后):
标准 iPhone iOS4 程序如下:
- 按“主页”按钮 - 应用程序转到后台
- 按“主页”按钮两次 - 出现正在运行的应用程序列表
- 点击并按住其中一个应用程序图标 - 每个图标上都会出现“摇动”和“-”号
- 点击应用程序图标上的“-”号 用户想要终止
用户必须< /strong> 每次他到达某个地方(例如一天两次)时终止我们的应用程序,以节省电池。如果他不这样做,GPS 就会很快耗尽电池电量。此外,我们无法在后台运行时禁用 GPS,因为我们需要精确的位置信息来实现主要应用程序功能。
当然,您无法更改应用程序中“主页”按钮的行为(被 AppStore 接受)。另外,我认为将预期行为更改为与 iPhone 平台上的其他应用程序完全不同的行为并不是一个好的解决方案。
你对此有何看法?不知道有什么好的解决办法吗。 我们有一个想法,但我还不想向您提出任何建议。
我们的解决方案的想法非常简单:简单地,在屏幕右上角显示小“x”按钮。点击它会终止应用程序(可能在出现一些确认问题后)。
该解决方案有一个很大的优点:它不会改变默认的系统行为 - 了解 iOS4 多任务处理使用情况的用户仍然可以按“home”按钮在后台运行应用程序并以 iPhone 的方式关闭它。
你怎么认为?
We're developing iPhone GPS application for car drivers.
As you probably know, iOS4 introduced multitasking, so our application can run in background - and it is. It's part of its functionality.
The problem is with standard method of closing applications on iOS4. Here are two scenarios:
1) User wants to put application to background:
Typically, on iPhone iOS4 it is as easy as pressing 'home' button.
2) User wants to turn application off (ie. after arriving to his destination point):
Standard iPhone iOS4 procedure is as follows:
- press 'home' button - application goes to the background
- press 'home' button twice - list of running applications appears
- tap and hold one of the applications icons - all icons 'shake' and '-' sign appears on each one
- tap '-' sign at the application icon user wants to terminate
User has to terminate our application each time he arrives somewhere (like, twice a day), to save his battery. If he does not - GPS would consume all the battery power really quickly. Also, we can't disable GPS while running in background, because we need precise location information for main application functionality.
Of course you can't change the behavior of 'home' button from your application (to be accepted to AppStore). Also I don't think it would be good solution to change expected behavior to something completely different than other applications on the iPhone platform.
What do you think about that? Do you know any good solution. We have one idea but I don't want to suggest anything to you yet.
Our idea for solution is very simple: Simply, show small 'x' button at the top right screen corner. Tapping it terminates the application (perhaps after some confirmation question).
This solution has one big advantage: it does not change default system behavior - users aware of iOS4 multitasking usage could still press 'home' button to run app in the background and close it iPhone-way.
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Apple 建议您这样做(iPhone 应用程序编程指南):
您是否尝试过通过调用 CLLocationManager 的
startMonitoringSignificantLocationChanges
方法来使用它?退出应用只能作为最后的手段。您可以在不使用定位服务时将其关闭,而无需退出应用。您可以在 CLLocationManager 中调用 stopUpdatingLocation 来将其关闭。
Apple recommends that you do this (iPhone App Programming Guide):
Have you tried using it by calling
startMonitoringSignificantLocationChanges
method of CLLocationManager?Exiting the app should only be done as a last resort. You can turn off location services when you're not using them, without exiting the app. You can call
stopUpdatingLocation
in CLLocationManager to turn it off.也许有一个潜在的解决方法?这实际上取决于您的应用程序。以下是我为在后台运行 GPS 设计的应用程序所做的工作。
我强烈建议您查看 CLLocationManager 中 iOS4 的
startMonitoringSignificantLocationChanges
API。驾驶时,它会为您提供每 2 公里左右的更新,精确到约 500 米。如果这对您的应用程序来说还不够,您只能在系统执行您的应用程序时打开 GPS 硬件。这样,您仍然可以每约 2 公里获得非常准确的读数,同时还能节省电池。
您还可以使用智能寻路和绘图技术来“伪造”丢失的数据。
我用 8 部 iPhone 3GS-es 进行了一项关于后台 GPS 事件的实验。结果发布在这里:iPhone后台GPS/signficantLocationChange事件初步分析
Maybe there is a potential workaround? It really depends on your app. Here's what I've done for the app I designed to run GPS in the background.
I highly recommend that you take a look at iOS4's
startMonitoringSignificantLocationChanges
API in CLLocationManager.When driving, it will give you an update that is accurate to about 500 meters every 2km or so. If this isn't sufficient for your application, you can power on the GPS hardware ONLY when your app is executed by the system. That way, you can still get very accurate readings every ~2km but save the battery.
You can use intelligent pathfinding and mapping techniques to "fake" missing data, as well.
I've conducted an experiment on background GPS events with 8 iPhone 3GS-es. The results are published here: iPhone background GPS/signficantLocationChange event preliminary analysis
为什么不创建一个按钮,让他们在到达目的地时按下,设置一个标志,表示“已到达”=“是”。
然后将其添加到应用程序中,这样当他们按下主页按钮时就会调用它。
当他们按下主页按钮时,这将有效地完全关闭应用程序,但前提是他们表明他们已经到达。否则它将处于后台模式。
Why don't you create a button that they can press when they arrive at their destination which would set a flag say Arrived = YES.
Then add this to the app so it will be called when they press the home button.
This will effectively fully shutdown the app when they press the home button but only if they've indicated that they have arrived. Otherwise it will be in background mode.
由于您需要在后台运行以实现定位目的,并且 Apple 不允许您终止自己的应用程序,因此用户必须了解他们需要自行终止应用程序以节省电量。
一个快速的解决方案是在到达目的地后关闭定位服务。您甚至可以提醒用户发生这种情况,一旦他们点击“确定”按钮,您就可以阻止它运行和/或在应用程序暂停时在后台运行。
Since you need to run in the background for location purposes, and Apple will not allow you to terminate your own app, you are stuck with the user having to understand that they need to terminate the app themselves to save power.
A quick solution would be to turn off location services once the destination is reached. You could even alert the user to this happening and once they hit the "Ok" button, you stop it from running and/or running in background if the app is suspended.
如果用户正在开车和移动,他们将需要准确的信息,这是事实。这并不意味着您也不能使用
startMonitoringSignificantLocationChanges
,不是吗?换句话说,当您在一段可调的时间长度内停止接收“重大变化”时,使用超时来准确地停止监控(并为 GPS 硬件供电)。当您再次收到它们时,再次开始准确监控。
If a user is driving and moving, they're going to need accurate information, true. That doesn't mean you can't use
startMonitoringSignificantLocationChanges
as well, does it?In other words, use a timeout to stop monitoring (and powering the GPS hardware) accurately when you've stopped receiving "significant changes" for a tune-able length of time. Start monitoring accurately again when you're receiving them again.
如果用户正在主动进行路线导航并进入后台,请继续使用 GPS;如果已到达目的地,请使用上面的建议并切换到
startMonitoringSignificantLocationChanges
或关闭位置服务。灰色区域是指用户接近目的地并且已完成 GPS 但尚未结束导航的情况。即他们的目的地不在 GPS 认为的位置等。现在您正在积极导航,但永远不会到达目的地。为了避免打扰用户或依赖某些设置,我建议观察无移动或至少无进展,然后将其用作用户不再依赖您的线索并切换到低功耗导航。If the user is actively on a route navigating and they go into background, keep using GPS, if they are at their destination, use the suggestions above and switch to
startMonitoringSignificantLocationChanges
or turn off location services. The gray area is if the user is close to their destination and are done with the GPS but haven't bothered to end navigating. i.e. their destination isn't right where the GPS thinks it is etc. Now you are actively navigating, but will never reach the destination. To avoid bothering the user or relying on some settings, I would suggest watching for non-movement or at least non-progress and then use that as a clue that the user isn't relying on you anymore and switch to low power navigating.我面前没有 Xcode(工作中的 PC),所以我无法查找您可以使用的任何方法,但我相信 TomTom 应用程序在后台运行时会进行某种监视,因为我记得收到了警报通知它要睡觉了。我记得这发生在 5-10 分钟后。
I don't have Xcode in front of me (PC at work) so I can't look up any methods you could use but I believe the TomTom app does some sort of monitoring when running in the background because I recall getting an alert notification that it was going to sleep. I recall this occurred after 5-10 minutes.
是否可以从应用程序内关闭 GPS 监控?您不能添加一个按钮来关闭它而不是退出整个应用程序吗? IE。如 TomTom 应用程序中所述,让应用程序进入睡眠状态。
如果是这样,您可能希望以某种方式更改 UI,以便用户可以看到 GPS 活动未运行,因此应用程序未发挥全部功能。
Is it possible to turn off the GPS monitoring from within the app? Could you not add a button to just turn that off instead of quitting the whole application? ie. Put the app to sleep as was mentioned about the TomTom app.
If so, you might then want to change the UI somehow so the user can see the GPS activity is not running, therefore the app is not at full functionality.
我有一个在类似应用程序中使用的解决方案。我的导航栏中有一个“背景模式”按钮。用户单击它,他们会收到通知,当他们点击主页时,他们将在后台运行。每当应用程序启动或重新激活时,再次点击主页将导致应用程序无法运行。
然后,当按下“主页”按钮时,我检查标志以查看是否必须进入后台模式。我只需禁用 GPS 即可避免后台模式(当在 UIbackgroundModes 中设置位置时,会导致应用程序挂起)
有意义吗?
I have a solution I'm using in a similar app. I have a "Background Mode" button in my nav bar. The user clicks it and they are notified that when they hit Home they will be running in the background. Any time the app is started or re-activated, hitting Home again will cause the app not to run.
Then, when the Home button is hit, I check the flag to see if we must go into background mode. I avoid background mode simply by disabling the GPS (which, when location is set it in UIbackgroundModes, causes the app to suspend)
Make sense?