際際滷

際際滷Share a Scribd company logo
Backgrounding
Bursting Your Bubble on iOS
Sorry



Doing what you want in the background
is almost impossible
photo by protographer23
Get Your Notepads
      Ready

Suspending vs. Resuming

Local Notifications

Doing Stuff in the Background
Exiting vs. Resume

Since iOS 4, you can keep your app
open on exit

But its really just a freeze and
resume - nothing happens while its
in the background
Resume Me



Set the UIApplicationExitsOnSuspend
to NO in your Info.plist
App Delegate Events
 App Initial Load -> Exit:

   applicationDidBecomeActive

   applicationWillResignActive

   applicationDidEnterBackground

 On Relaunch:

   applicationWillEnterForeground
Sounds Promising

applicationDidEnterBackground sounds
promising, but you cant do anything
inside it that is long-running.

  Docs say you have 5 seconds

  You cant get back to the UI
Terminate on Exit



Set UIApplicationExitsOnSuspend to
YES in your Info.plist
App Delegate Events

 App Launch -> Exit:

   applicationDidBecomeActive

   applicationDidEnterBackground

   applicationWillTerminate
Kill Me



If the user does the jiggle and Red-
Xes the app, no events are fired
DEMO!
Local Notifications

You can schedule local notifications
to prompt the user at a future time
even if your app isnt running

Useful - but remember the user isnt
required to open the app, and your
notification may never appear
DEMO!
Ok, Next

So you can do things in the
background, but its limited to 3
things:

  Get GPS updates

  Play Justin Bieber

  Do the VOIP
Set Your Plist

Do do one of those 3 lovely things in
the background, set the
UIBackgroundModes in your
Info.plist

Values are: location, audio, voip
Location


Get super duper real-time updates or
major changes only

Use CLLocationManager to do the magic
DEMO!
Playing Audio


Its easy: start playing your tunes
using AV* framework.

If something else starts playing
music, you lose
DEMO!
VOIP


Sorry, no demo.

But supposedly you can open a socket
and keep it open.
Fighting the Man

People have tried tricking the system
by playing an empty sound file.
This works until some other app plays
sound.

GPS Updates take up battery power, so
your users may hate you.
Conclusion
Read more at:

  http://developer.apple.com/library/ios/
  #documentation/iphone/conceptual/
  iphoneosprogrammingguide/
  BackgroundExecution/
  BackgroundExecution.html

Sample Code: https://github.com/
dnstevenson/BackgroundTest

Good Luck!
Thanks!

Find me on Twitter @dnstevenson

Buy one of my apps at http://
www.stevensonsoftware.com

Leave me a 5-star review and Ill be
your best friend

More Related Content

iOS Backgrounding - SLC Cocoaheads

  • 2. Sorry Doing what you want in the background is almost impossible
  • 4. Get Your Notepads Ready Suspending vs. Resuming Local Notifications Doing Stuff in the Background
  • 5. Exiting vs. Resume Since iOS 4, you can keep your app open on exit But its really just a freeze and resume - nothing happens while its in the background
  • 6. Resume Me Set the UIApplicationExitsOnSuspend to NO in your Info.plist
  • 7. App Delegate Events App Initial Load -> Exit: applicationDidBecomeActive applicationWillResignActive applicationDidEnterBackground On Relaunch: applicationWillEnterForeground
  • 8. Sounds Promising applicationDidEnterBackground sounds promising, but you cant do anything inside it that is long-running. Docs say you have 5 seconds You cant get back to the UI
  • 9. Terminate on Exit Set UIApplicationExitsOnSuspend to YES in your Info.plist
  • 10. App Delegate Events App Launch -> Exit: applicationDidBecomeActive applicationDidEnterBackground applicationWillTerminate
  • 11. Kill Me If the user does the jiggle and Red- Xes the app, no events are fired
  • 12. DEMO!
  • 13. Local Notifications You can schedule local notifications to prompt the user at a future time even if your app isnt running Useful - but remember the user isnt required to open the app, and your notification may never appear
  • 14. DEMO!
  • 15. Ok, Next So you can do things in the background, but its limited to 3 things: Get GPS updates Play Justin Bieber Do the VOIP
  • 16. Set Your Plist Do do one of those 3 lovely things in the background, set the UIBackgroundModes in your Info.plist Values are: location, audio, voip
  • 17. Location Get super duper real-time updates or major changes only Use CLLocationManager to do the magic
  • 18. DEMO!
  • 19. Playing Audio Its easy: start playing your tunes using AV* framework. If something else starts playing music, you lose
  • 20. DEMO!
  • 21. VOIP Sorry, no demo. But supposedly you can open a socket and keep it open.
  • 22. Fighting the Man People have tried tricking the system by playing an empty sound file. This works until some other app plays sound. GPS Updates take up battery power, so your users may hate you.
  • 23. Conclusion Read more at: http://developer.apple.com/library/ios/ #documentation/iphone/conceptual/ iphoneosprogrammingguide/ BackgroundExecution/ BackgroundExecution.html Sample Code: https://github.com/ dnstevenson/BackgroundTest Good Luck!
  • 24. Thanks! Find me on Twitter @dnstevenson Buy one of my apps at http:// www.stevensonsoftware.com Leave me a 5-star review and Ill be your best friend

Editor's Notes