Day 46: Stanford CS193p – Application Lifecycle and Core Motion

Day 46: Stanford CS193p – Application Lifecycle and Core Motion

Screen Shot 2015-06-26 at 7.57.36 PM

Lecture 13

By this point I’ve had plenty of exposure and practice with NSNotifications and Application Lifecycle, but it was great to have a review on it. There were plenty of details that were new to me, especially with NSNotifications. There is plenty left to learn on this particular topic.

Model to Controller communication
Global to Controller communication

Screen Shot 2015-06-26 at 7.58.37 PM

Application Lifecycle:
The run down
Screen Shot 2015-06-26 at 8.05.29 PM

Starts not running
Foreground, starts inactive, no UI events
moves to state of active (normal mode), where most coding is
Background is brief, coding running in background before not running
Then suspended, no longer running

Can run in background periodically, run shortly to fetch updated data

Launching is running in the foreground
Switch to another application SHOT
Each time you run through orange lines, you get notified


AppDelegate will receive:
func application(UIApplication, didFinishLaunchingWIthOptions: [NSObject: AnyObject])

and then observe:

The passed dictionary tells why app was launched

Then once you are done using the app, it goes into background:
only runs 30 seconds or so
can request more time, but not too much, otherwise system will kill you off
prepare to be killed off

Then, AppDelegate will receive:
func applicationWillEnterForeground(UIApplication)
and you can observe:

When you’re not killed off, the cycle can continue
time to run un-batten the hatches
maybe undue whats in DidEnterBackground
then made Active
back in foreground 🙂

That’s the cycle

AppDelegate can do more:
Screen Shot 2015-06-26 at 8.22.21 PM

There are cases of being aloud to run in background, such as the music app, but there are extra steps to be allowed to do that. Must follow “good citizen” standards.

Shared Instance:
single instance in app: UIApplication
let myApp = UIApplication.sharedApplication()
manages all global behavior
never needs to be subclassed
delegates everything needed to be involved in to its UIApplicationDelegate

It can:

Opening a URL in another Application:
func openURL(NSURL)
func canOpenURL(NSURL) -> Bool

Registering or Scheduling Notifications (Push or Local)
func (un)registerForRemoteNotifications()
func scheduleLocalNotifcation(UILocalNotification)
func registerUserNotificationSettings(UIUserNotificationSettings)

Setting the fetch interval for background fetching:
must set
func setMinimumBackgroundFetchInterval(NSTimerInterval)
Usually set to: UIApplicationBackgroundFetchIntervalMinimum

Asking for more time when backgrounded:
func backgroundTaskWithExpirationHandler(handler: () -> Void) UIBackgroundTaskIdentifier
don’t forget to call:
when done!

Turning on the “network in use” spinner (status bar upper left)
var networkActivityIndicatorVisible: Bool

Finding out about things:
var backgroundTimeRemaining: NSTimeInterval { get } //until suspended
var preferredContentSizeCategory: String { get } //big fonts or small fonts
var applicationState: UIApplicationState { get } //foreground, background, active

Then went into Info.plist:
Screen Shot 2015-06-26 at 8.42.42 PM

editable in settings, click on project in Navigator

Some features require enabling:
iCloud, Game Center, HomeKit, MapKit, etc

Switch these on in Capabilities tab, inside Project Settings

First stage of AirDrop demo:
Screen Shot 2015-06-26 at 8.49.13 PM

Importing vacation info of hike, using GPX files! Very cool!!

Dropping GPX file from Mac to iPhone


Published by: Travis Deaton

I studied International Relations at American University, volunteered for various organizations, and have recently returned from a 5 month tour of our National Parks. Currently an EMT/Wildland Fire & SAR student.

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s