Please note, this blog entry is from a previous course. You might want to check out the current one.
NSNotification
Notifications provide a way to react to asynchronous events. e.g.:
[swift]
NSNotificationCenter.defaultCenter()
func addObserverForName(String, // name of the "radio station"
object: AnyObject?, // broadcaster (or nil for "anyone")
queue: NSOperationQueue?) // queue to execute the closure on
{ (notification: NSNotification) -> Void in
let info: [NSObject:AnyObject]? = notification.userInfo // notification-specific information
}
[/swift]
Application Lifecycle
An app has four different states:
When in foreground, the app can either be inactive (running, but no UI events) or active (running, receiving and processing UI events).
In background code can run, but only for a limited time and without UI events.
When suspended, no code is running, and the app can be killed at any time.
When launching, the app goes from not running to inactive – the application delegate is called with didFinishLaunchingWithOptions, UIApplicationDidFinishLaunchingNotification is sent to any observer – and then to active – the application delegate is called with applicationDidBecomeActive, UIApplicationDidBecomeActiveNotification is sent to any observer.
When switching to another app, the app goes from active to inactive – the application delegate is called with applicationWillResignActive,UIApplicationWillResignActiveNotification is sent to any observer – to background – the application delegate is called with applicationDidEnterBackground, UIApplicationDidEnterBackgroundNotification is sent to any observer – and then to suspended.
When an app switches from background to foreground, the application delegete is called with applicationWillEnterForeground, UIApplicationWillEnterForegroundNotification is sent to any observer.
When an app gets killed it goes from suspended to not running – without any notification.
Other methods of the delegation methods are
- local notifications – timers set to go off at certain times to wake the app if needed
[swift]
func (un)registerForRemoteNotifications()
func scheduleLocalNotification(UILocalNotification)
func registerUserNotificationSettings(UIUserNotificationSettings)
[/swift] - state restoration – saving the state of the UI to be able to restore it even if the app was killed
- data protection – protecting files when the device’s screen is locked
- open URL – opening registered for URLs
[swift]
func openURL(NSURL)
func canOpenURL(NSURL) -> Bool
[/swift] - background fetching – fetching and receiving results in the background
[swift]
func setMinimumBackgroundFetchInterval(NSTimeInterval) // UIApplicationBackgroundFetchIntervalMinimum
func backgroundTaskWithExpirationHandler(handler: () -> Void) -> UIBackgroundTaskIdentifier
endBackgroundTask(UIBackgroundTaskIdentifier)
[/swift] - …
[swift]
var networkActivityIndicatorVisible: Bool
var backgroundTimeRemaining: NSTimeInterval { get }
preferredContentSizeCategory: String { get }
var applicationState: UIApplicationState { get }
[/swift]
The just learned – including manipulating Info.plist – is shown in the Airdrop demo Trax.
Core Motion
- Check which hardware is available.
[swift]
var {accelerometer,gyro,magnetometer,deviceMotion}Available: Bool
[/swift] - Start sampling ans poll the motion manager for the latest result.
[swift]
func start{Accelerometer,Gyro,Magnetometer,DeviceMotion}Updates()
var {accelerometer,gyro,magnetometer,deviceMotion}Active: Bool
var accelerometerData: CMAccelerometerData
var acceleration: CMAcceleration
var gravity: CMAcceleration
var userAcceleration: CMAcceleration
struct CMAcceleration {
var x: Double // in g (9.8m/s/s)
var y: Double // in g
var z: Double // in g
}var gyroData: CMGyroData
var rotationRate: CMRotationRate
struct CMRotationRate {
var x: Double // in radians/s
var y: Double // in radians/s
var z: Double // in radians/s
}
var attitude: CMAttitude
class CMAttitude: NSObject {
var roll: Double
var pitch: Double
var yaw: Double
}
var magneticField: CMCalibratedMagneticField
struct CMCalibratedMagneticField {
var field: CMMagneticField
var accuracy: CMMagneticFieldCalibrationAccuracy
}var magnetometerData: CMMagnetometerData
var magneticField: CMMagneticField
struct CMMagneticField {
var x: Double // in microteslas
var y: Double // in microteslas
var z: Double // in microteslas
}
[/swift] - Stop sampling.
[swift]
func stop{Accelerometer,Gyro,Magnetometer,DeviceMotion}Updates()
[/swift]
or:
- Set the rate for receiving new values
[swift]
var accelerometerUpdateInterval: NSTimeInterval
var gyroUpdateInterval: NSTimeInterval
var magnetometerUpdateInterval: NSTimeInterval
var deviceMotionUpdateInterval: NSTimeInterval
[/swift] - Register a closure to use those values
[swift]
func startAccelerometerUpdatesToQueue(queue: NSOperationQueue!,
withHandler: CMAccelerometerHandler)
typealias CMAccelerationHandler = (CMAccelerometerData!, NSError!) -> Voidfunc startDeviceMotionUpdatesToQueue(queue: NSOperationQueue!,
withHandler: CMDeviceMotionHandler)
typealias CMDeviceMotionHandler = (CMDeviceMotion!, NSError!) -> Void
[/swift]
Core Motion is demonstrated in the Bouncer demo.
The lecture and its slides are available via iTunes named “13. Application Lifecycle and Core Motion”. The code for Trax is available on GitHub and at Stanford. The code for Bouncer is available on GitHub and at Stanford













