开发者

Designing iPhone iOS4 application behavior: running in background vs. terminating application

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?


Apple recommends that you do this (iPhone App Programming Guide):

Applications can register for significant location changes only. (Recommended) The significant-change location service is available in iPhone OS 4 and later for devices with a cellular radio. It offers a low-power way to receive location data and is highly recommended. New location updates are provided only when the user’s position changes significantly. If the application is suspended while running this service, new location updates will cause the application to be woken up in the background to handle them. Similarly, if the application is terminated while running this service, the system relaunches the application automatically when new location data becomes available.

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.

- (void)stopUpdatingLocation

You should call this method whenever your code no longer needs to receive location-related events. Disabling event delivery gives the receiver the option of disabling the appropriate hardware (and thereby saving power) when no clients need location data. You can always restart the generation of location updates by calling the startUpdatingLocation method again.


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.

- (void)applicationWillResignActive:(UIApplication *)application {
    if (Arrived) {
       exit(0);
    }
}

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.


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.


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.


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.


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜