iPhone Life magazine

Another great feature of iPhone OS 4: real(!) suspension in the background (demo video & source code!)

As I've been selected to lead the iPhone training courses for a big lecturing company, I've been really busy preparing my own material for it and leading initial test courses to get the material right and well-taught. (At the moment, I'm around at page 700 and have already discussed a lot of stuff like Xcode's secrets, graphics, networking and the like. I've prepared all the sample programs and screenshots in English so that I can, when there is need for it, more easily translate the entire stuff into English.)


During this, I've also elaborated on the new multitasking features of iPhone OS4. In a nutshell: if your application is specifically compiled for OS4, it will not be terminated, but just suspended when you double-press the Home button and switch to some other app or press and hold the Home key for about 3 seconds (like was the case with Backgrounder on jailbroken phones). Unless, of course, it's either streaming from a Web source (think of radio apps), collect location data or is in a VoIP session. Other types of network connections aren't supported; e.g., „plain” socket connections like those necessitated by IRC clients (a dedicated problem report is at http://openradar.appspot.com/radar?id=263401).

Of special interest, at least for the folks that used to use Backgrounder to quickly background their third-party (non-Safari) Web browser while doing something else in another app, is real(!) suspension. If you read the updates posted at 8/25 and 8/26/2009 in the first version of my Web browser roundup at http://www.iphonelife.com/blog/87/one-and-only-real-iphone-web-browser-roundup , you'll notice Backgrounder doesn't suspend backgrounded apps – they continue to suck up CPU cycles (and, consequently, battery power) and, in some cases, use data. This not only applies to Web browsers continuously refreshing Web pages, but also, for example, games, continuously running in the background when it's not at all needed.

Now, if you do compile a project in Xcode for OS4 and do make you deployed app on your 3G S running the latest (currently, beta3) OS4 release, you'll notice it no longer requests pages using the “Refresh” meta-tag when in background, unlike back in the Backgrounder days. And, of course, the case is the same with all the other stuff.

Unfortunately, only OS4 projects will be suspended, any apps built for previous OS versions will immediately be terminated, including all the commercial (and free) apps available in the AppStore. This is pretty much understandable as, currently, it's forbidden to post any OS4 apps in the AppStore. That is, you still can't really use this feature unless you write your own app.

Nevertheless, let me show you how this is done. The following code doesn't contain any OS4-specific code; the only thing to keep in mind is compiling it for OS4 and not 3.2, the other available target in the current (preview) version of Xcode 3.2.3.

The WebViewDemo3ViewController.h file:

#import

@interface WebViewDemo3ViewController : UIViewController {
UIWebView *myWebView;
}
@property (retain) IBOutlet UIWebView *myWebView;
-(IBAction) buttonCallback;
@end


The relevant sections in the WebViewDemo3ViewController.m file:

#import "WebViewDemo3ViewController.h"

@implementation WebViewDemo3ViewController
@synthesize myWebView;

-(IBAction) buttonCallback
{
NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.1.102:8088/refr.jsp"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
[myWebView loadRequest: request];
[url release];
}

...

where change “http://192.168.1.102:8088/refr.jsp to the URL you run your server-side tester on. Drop a UIWebView and a button on a View-Based template-based project, connect the myWebView outlet, deploy the app, start it and tap the button. After this, try both suspending the entire phone (the execution will go on) and sending the app into the background (3-sec-long or double Home press). With the latter, the server won't report any refreshes any more – unlike when using Backgrounder.

The video demonstrating the results is at http://www.youtube.com/watch?v=KPXO5ERN_sw . After starting the app, at 0:05, I press the button at the top end to connect to my script I also used in my previous article at http://www.iphonelife.com/blog/87/one-and-only-real-iphone-web-browser-roundup, with an additional out.println(System.currentTimeMillis()); command at the end so that the client also displays something, showing there is constant refreshing going on.

At 0:12, I suspend the entire phone to show the refreshes just go on, as can clearly be seen from the background window, where the numbers continue to run upwards.

At 0:16, I resume the iPhone. No changes – the app continues running as before.

At 0:24, I double-press Home to bring up the task switcher. There, I select the AppStore app. As you can see, there're no numbers running in the background window, showing the app has indeed been suspended – unlike when using Backgrounder.

At 0:31, I double-press Home again to bring up the task switcher; there, I re-select my small Web browser, which resumes its refreshes. At 0:34, I switch to Settings; you can clearly see my Web browser stops running. At 0:40, I switch back to my app, which resumes running.

At 0:47, I press and hold the Home button to background the app without quitting it. Refreshing stops. After I resume it (from the Springboard), you can see it continues on where it was left – that is, I didn't need to tap the button at the top for it to resume working.

All in all, the new suspension feature of OS4 is just great. You no longer need to be afraid of your (backgrounded) native OS4 apps to chew through your battery, while you can quickly go on with any task you've been doing / game you've been playing before backgrounding the app. Again, this will, for the time being, work only with apps you write and compile for yourself, but as soon as Apple lets native OS4 apps in the AppStore, this will change.

BTW, should I receive positive feedback on this article, I may post installments to it showing you examples of truly multitasking code – that is, not only a demo showing suspension, but also code actively making use of background execution like streaming audio.

 

Email icon
Want more? Get our weekly newsletter:

Werner Ruotsalainen is a frequent contributor to several Pocket PC communities, including the Smartphone & Pocket PC magazine blog. He has a degree in CS/EE and is working as an IT consultant. His favorite pastime is cross-country skiing, swimming and Nordic walking. He lives in Europe with his wife. You can contact Werner at infoatwinmobiletech [dot] com (info@winmobiletech.com).

From Our Store

Comments

Looking forward to multitasking

One of the nice things about my iPad is that the apps open so much more quickly. But there's still an annoying delay, and I'm really looking forward to multitasking. I hope it comes to the iPad soon, but apparently it won't be until fall.

Thanks for the detail on how it works.

Jim

Thanks

Thank you so much for detailing this all for us. It will be interesting to see how it works and how much faster we can multi-task in our busy daily schedules. We will continue to see what else you have to offer. The time to gamble at an ... casino is not when you are broke because you may end up wagering too much for your own good. | online casino

Thanks guys! In addition to

Thanks guys! In addition to some major iPad compliance (and, when it becomes available here in Europe, iPhone 4G)-specific updates to my past articles, I plan to start a strictly programming & developing-related blog here. I have a *lot* to speak on; for example, a lot of pretty weakly documented 3.2+ stuff. (For example, there isn't a decently explained(!) UIGestureRecognizer tutorial anywhere - Apple still haven't published their iPad Programming Guide, so, one needs to do a lot of experimenting and API reading to find out how it works. And it's only one example of the new and partly undocumented stuff already available for iPad users/programmers.) I have a lot of unique code examples too; for example, I've converted Apple's MoviePlayer example to run on the iPad / iPhone OS 4. (The officially available one isn't compatible with these platforms because of the major changes in the API.) I will surely post my small app that plays videos with subtitles and uses File Sharing for you 3.2+ users (for pre-3.2 users, I have a Webserver-based file transfer module, but it's at least 12-15 times slower than File Sharing in 3.2+), should you want to avoid having to pay $20 for iSubtitle, the, currently, only one app to produce iPhone-friendly video files with real subtitles (and not just burnt-in ones). I also plan to publish some real code examples (Apple, as usual, only have documentation without real examples) of the brand new multitasking feature of OS4.