iPhoneAppProgrammingGuide.pdf

(1573 KB) Pobierz
App Programming
Guide for iOS
Contents
About iOS App Architecture
8
At a Glance
8
Apps Are Expected to Support Key Features
9
Apps Follow Well-Defined Execution Paths
9
Apps Must Run Efficiently in a Multitasking Environment
9
Communication Between Apps Follows Specific Pathways
9
Performance Tuning is Important for Apps
10
How to Use This Document
10
Prerequisites
10
See Also
10
Expected App Behaviors
11
Providing the Required Resources
11
The App Bundle
12
The Information Property List File
15
Declaring the Required Device Capabilities
16
App Icons
16
App Launch (Default) Images
17
Supporting User Privacy
17
Internationalizing Your App
20
The App Life Cycle
22
The Main Function
22
The Structure of an App
23
The Main Run Loop
25
Execution States for Apps
27
App Termination
30
Threads and Concurrency
30
Background Execution
32
Executing Finite-Length Tasks
33
Downloading Content in the Background
34
Implementing Long-Running Tasks
35
Declaring Your App’s Supported Background Tasks
36
2014-09-17 | Copyright © 2014 Apple Inc. All Rights Reserved.
2
Contents
Tracking the User’s Location
37
Playing and Recording Background Audio
38
Implementing a VoIP App
39
Fetching Small Amounts of Content Opportunistically
40
Using Push Notifications to Initiate a Download
41
Downloading Newsstand Content in the Background
41
Communicating with an External Accessory
42
Communicating with a Bluetooth Accessory
42
Getting the User’s Attention While in the Background
43
Understanding When Your App Gets Launched into the Background
44
Being a Responsible Background App
45
Opting Out of Background Execution
47
Strategies for Handling App State Transitions
48
What to Do at Launch Time
48
The Launch Cycle
49
Launching in Landscape Mode
53
Installing App-Specific Data Files at First Launch
54
What to Do When Your App Is Interrupted Temporarily
54
Responding to Temporary Interruptions
56
What to Do When Your App Enters the Foreground
57
Be Prepared to Process Queued Notifications
58
Handle iCloud Changes
59
Handle Locale Changes
60
Handle Changes to Your App’s Settings
60
What to Do When Your App Enters the Background
60
The Background Transition Cycle
61
Prepare for the App Snapshot
63
Reduce Your Memory Footprint
63
Strategies for Implementing Specific App Features
65
Privacy Strategies
65
Protecting Data Using On-Disk Encryption
65
Identifying Unique Users of Your App
66
Supporting Multiple Versions of iOS
67
Preserving Your App’s Visual Appearance Across Launches
68
Enabling State Preservation and Restoration in Your App
69
The Preservation and Restoration Process
69
What Happens When You Exclude Groups of View Controllers?
80
Checklist for Implementing State Preservation and Restoration
83
2014-09-17 | Copyright © 2014 Apple Inc. All Rights Reserved.
3
Contents
Enabling State Preservation and Restoration in Your App
84
Preserving the State of Your View Controllers
84
Preserving the State of Your Views
88
Preserving Your App’s High-Level State
91
Tips for Saving and Restoring State Information
91
Tips for Developing a VoIP App
92
Configuring Sockets for VoIP Usage
93
Installing a Keep-Alive Handler
95
Configuring Your App’s Audio Session
95
Using the Reachability Interfaces to Improve the User Experience
95
Inter-App Communication
97
Supporting AirDrop
97
Sending Files and Data to Another App
97
Receiving Files and Data Sent to Your App
98
Using URL Schemes to Communicate with Apps
99
Sending a URL to Another App
99
Implementing Custom URL Schemes
99
Displaying a Custom Launch Image When a URL is Opened
104
Performance Tips
106
Reduce Your App’s Power Consumption
106
Use Memory Efficiently
108
Observe Low-Memory Warnings
108
Reduce Your App’s Memory Footprint
109
Allocate Memory Wisely
109
Tune Your Networking Code
110
Tips for Efficient Networking
110
Using Wi-Fi
111
The Airplane Mode Alert
111
Improve Your File Management
112
Make App Backups More Efficient
112
App Backup Best Practices
112
Files Saved During App Updates
114
Move Work off the Main Thread
114
Document Revision History
115
Swift
7
2014-09-17 | Copyright © 2014 Apple Inc. All Rights Reserved.
4
Figures, Tables, and Listings
Expected App Behaviors
11
Table 1-1
Table 1-2
A typical app bundle
12
Data protected by system authorization settings
19
The App Life Cycle
22
Figure 2-1
Figure 2-2
Figure 2-3
Table 2-1
Table 2-2
Table 2-3
Listing 2-1
Key objects in an iOS app
23
Processing events in the main run loop
26
State changes in an iOS app
29
The role of objects in an iOS app
24
Common types of events for iOS apps
26
App states
28
The
main
function of an iOS app
22
Background Execution
32
Table 3-1
Listing 3-1
Listing 3-2
Background modes for apps
36
Starting a background task at quit time
33
Scheduling an alarm notification
43
Strategies for Handling App State Transitions
48
Figure 4-1
Figure 4-2
Figure 4-3
Figure 4-4
Figure 4-5
Table 4-1
Launching an app into the foreground
50
Launching an app into the background
52
Handling alert-based interruptions
56
Transitioning from the background to the foreground
57
Moving from the foreground to the background
62
Notifications delivered to waking apps
58
Strategies for Implementing Specific App Features
65
Figure 5-1
Figure 5-2
Figure 5-3
Figure 5-4
Figure 5-5
Figure 5-6
Table 5-1
A sample view controller hierarchy
71
Adding restoration identifies to view controllers
74
High-level flow interface preservation
76
High-level flow for restoring your user interface
78
Excluding view controllers from the automatic preservation process
81
Loading the default set of view controllers
82
Configuring stream interfaces for VoIP usage
94
2014-09-17 | Copyright © 2014 Apple Inc. All Rights Reserved.
5
Zgłoś jeśli naruszono regulamin