Escolar Documentos
Profissional Documentos
Cultura Documentos
In Mac OS X Lion
Session 119
Peter Ammon David Smith
Cocoa Frameworks Engineer Cocoa Frameworks Engineer
These are confidential sessionsplease refrain from streaming, blogging, or taking pictures
1
Resume and Automatic Termination
2
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
3
Resume
In Mac OS X Lion
Peter Ammon
Cocoa Frameworks Engineer
4
Apps simply
Resume
where they left off
after quit, log out, or crash
5
Demo
6
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
7
Why Resume?
Why do I want to do this?
Simplifying the application model
Users will expect that the application state is not lost
Macs will be able to silently restart without the user noticing
8
Why Resume?
Why should I use the Cocoa APIs for this?
There is a lot to restore
A window has a frame, on a display, on a space,
or maybe its minimized, or full screen
It integrates with the rest of the system
Inter-application Z-order
Shift key
9
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
10
Overview of Resume
11
Overview of Resume
12
Overview of Resume
13
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
14
Recreating Open Windows
API summary
Mark a window as restorable
[window setRestorable:YES]
15
Recreating Open Windows
API summary
+ (void)restoreWindowWithIdentifier:(NSString *)identifier
! ! state:(NSCoder *)state
! ! completionHandler:(void (^)(NSWindow *, NSError *)handler
16
Restoring Windows
NSFontPanel example
[fontPanel setRestorable:YES];
[fontPanel setRestorationClass:[fontPanel class]];
+ (void)restoreWindowWithIdentifier:(NSString *)identifier
! ! ! state:(NSCoder *)state
! ! ! completionHandler:(void (^)(NSWindow *, NSError *)handler {
! ! ! ! handler([self sharedFontPanel], NULL);
! ! ! }
17
Restoring Windows
NSDocument integration
NSDocument sets its windows restoration class to the
NSDocumentController
NSDocumentController reopens windows by reopening
their documents
Customizable hooks
@implementation MyDocument
MyDocumentController
- (void)restoreDocumentWindowWithIdentifier:(NSString *)identifier
(void)restoreWindowWithIdentifier:(NSString *)identifier
state:(NSCoder *)state
completionHandler:(void (^)(NSWindow *, NSError *))handler
@end
18
Demo
19
Restoring Windows
Which windows should be restorable?
Most windows should be restorable, with some exceptions
Transient windows
Tooltips, shielding windows
Windows the user does not want to restore
Private browsing in Safari
Windows whose job is done
Install Complete window
20
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
21
Restoring State Within Windows
Each component within the window has its own private state
NSView, NSWindow, NSWindowController, NSDocument
NSApplication, too
At some point later, the component will be asked to encode its state
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
22
Restoring State Within Windows
23
Restoring State Within Windows
What state should be restored via this mechanism?
Restore view and controller state
Not model state
Selected range
Text
Scroll position
24
Demo
25
Restoring Windows
Complex cases
My window needs to know its state before I can even create it!
Multiple types of windows or documents
+ (void)restoreWindowWithIdentifier:(NSString *)identifier
! ! ! state:(NSCoder *)state
! ! ! completionHandler:(void (^)(NSWindow *, NSError *)handler;
26
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
27
Advanced Topics
Inter-view references
How NSWindow records its first responder:
[coder encodeObject:firstResponder forKey:@"NSFirstResponder"]
28
Advanced Topics
Sandboxed apps
Encode URLs to files in any restorable state NSCoder
You automatically get permission to reopen them
Even if they have been moved or renamed
URL encoding uses bookmarks
Or use NSDocument
29
Advanced Topics
Case study: Mail
Mail already restored its state via state from NSUserDefaults
Integrating with Resume was ~ 60 LOC
Idea
Gave each window a unique ID
Recorded it in both the user default record and the restorable state
30
Best Practices
Do not assume that if your app is being launched, the user intends
to use it immediately
Running applications get relaunched at login
31
Best Practices
32
Best Practices
33
Best Practices
But if you already restore state, its easy to integrate with Resume
34
Resume Summary
35
Automatic Termination
In Mac OS X Lion
David Smith
Cocoa Frameworks Engineer
36
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
37
What Is Automatic Termination?
38
What Is Automatic Termination?
39
The Application Life Cycle
circa 3000BC (Snow Leopard)
Use
Launch Quit
40
The Application Life Cycle
circa 2011 (Lion)
Use
Launch Quit
41
What Is Automatic Termination?
42
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
43
Benefits of Automatic Termination
44
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
45
API Overview
46
An Important Aside
47
API Overview
<key>NSSupportsAutomaticTermination</key>
<true/>
OR use NSProcessInfo
[[NSProcessInfo processInfo] setAutomaticTerminationSupportEnabled: YES];
48
API Overview
49
Running Applications with No Process
50
Processes with No Running Application
The app will appear to quit, but its process will remain running
This lets it relaunch instantly
It will terminate if the system needs to reclaim its resources
51
Demo
52
Testing Automatic Termination in Your App
53
Recap: Adopting Automatic Termination
54
Resume and Automatic Termination
Resume
Why Resume?
API overview
Automatic Termination
What is Automatic Termination?
API overview
Future directions
55
Imagine a World
56
More Information
Bill Dudney
Application Frameworks Evangelist
dudney@apple.com
Documentation
Mac OS X Dev Center
http://developer.apple.com/devcenter/mac
57
Related Sessions
Presidio
Whats New in Cocoa Tuesday 10:15AM
Pacific Heights
Auto Save and Versions in Mac OS X 10.7 Lion Tuesday 3:15PM
58
Labs
59
60