Responding to the Launch of Your App Initialize your app’s data structures, prepare your app to run, and respond to any launch-time requests from the ...
Responding to the Launch of Your App
Initialize your app’s data structures, prepare your app to run, and respond to any launch-time requests from the system.
初始化應用程式的數據結構、準備運行應用程式、響應系統啟動時的一些請求。
Overview
The system launches your app when the user taps your app’s icon on the Home screen. If your app requested specific events, the system might also launch your app in the background to handle those events. For a scene-based app, the system similarly launches the app when one of your scenes needs to appear onscreen or do some work.
當用戶點擊桌面的應用程式圖標時,系統就會運行你的應用程式,如果你的應用程式請求特定的事件,系統可能會在後臺進行處理這些事件,而對於一個基於場景的應用程式,當一個場景需要出現在屏幕上或者做一些工作時,系統也會啟動應用程式。
All apps have an associated process, represented by the UIApplication
object. Apps also have an app delegate object—an object that conforms to the UIApplicationDelegate
protocol—whose job is to respond to important events happening within that process. Even a scene-based app uses an app delegate to manage fundamental events like launch and termination. At launch time, UIKit automatically creates the UIApplication
object and your app delegate. It then starts your app’s main event loop.
所有的應用程式都有一個關聯的進程,由UIApplication對象標識,應用程式也有一個委托對象(一個基於UIApplicationDelegate協議的對象,它的工作時響應進程執行中的重要事件),甚至一個基於場景的應用程式也使用應用程式委托對象來管理基本事件,在啟動時,UIKit會自動創建UIApplication對象和委托代理。然後啟動應用程式的主事件迴圈。
Provide a Launch Storyboard
When the user first launches your app on a device, the system displays your launch storyboard until your app is ready to display its UI. Displaying the launch storyboard assures the user that your app launched and is doing something. If your app initializes itself and readies its UI quickly, the user may see your launch storyboard only briefly.
當用戶第一次在設備上啟動你的應用程式時,系統會顯示啟動故事板,直到程式準備好它的UI.顯示故事板的目的是向用戶保證你的應用程式已經啟動,而且正在運行。 如果你的應用程式初始化的速度和準備的過程非常快,那用戶可能只能短暫的看到你的啟動故事板。
Xcode projects automatically include a default launch storyboard for you to customize, and you can add more launch storyboards as needed. To add new launch storyboards to your project, do the following:
Xcode項目會自動包含一個預設的啟動故事板,但你可以根據需要添加更多的故事板,如果想添加新的啟動故事板到項目中,需要做如下操作:
-
Open your project in Xcode.
-
Select File > New > New File.
-
Add a Launch Screen resource to your project.
Add views to your launch storyboard, and use Auto Layout constraints to size and position those views so that they adapt to the underlying environment. For example, you might create a splash screen with a solid-color background and a fixed-size image of your company logo or other content. Always create a final version of the interface you want. Never include temporary content or content that requires localization. UIKit displays exactly what you provide, using your constraints to fit your views into the available space.
添加視圖到啟動故事板中,使用自動佈局約束來調整視圖的大小和位置,以適應底層的環境。例如:你可以創建一個純色的背景和固定大小的圖片來標識你的公司logo,或者其他內容。始終創建所需介面的最終版本。不要包含臨時內容或需要本地化的內容。精確地顯示你所提供的,使用你的約束來講你的視圖放入可用的空間。
Important
In iOS 13 and later, always provide a launch storyboard for your app. Don’t use static launch images.
在iOS13及以後,總是提供一個故事板,不再使用靜態的啟動圖片。
Initialize Your App's Data Structures
Put your app's launch-time initialization code in one or both of the following methods:
將您的應用程式的啟動時初始化代碼放在以下一個或兩個方法:
UIKit calls these methods at the beginning of your app’s launch cycle. Use them to:
UIKit在應用程式啟動周期開始時調用這些方法。使用它們來做以下操作:
-
Initialize your app's data structures.
- 初始化應用程式的結構
-
Verify that your app has the resources it needs to run.
- 驗證應用程式運行所需要的資源
-
Perform any one-time setup when your app is launched for the first time. For example, install templates or user-modifiable files in a writable directory; see Performing One-Time Setup for Your App.
- 在應用程式第一次啟動時執行任何一次性設置,例如:在可讀寫的目錄中安裝模板或用戶可修改的文件。
-
Connect to any critical services that your app uses. For example, connect to the Apple Push Notification service if your app supports remote notifications.
- 連接應用程式使用的任何關鍵服務。例如:如果你的應用程式支持遠程推送通知,會連接到APNS.
-
Check the launch options dictionary for information about why your app was launched; see Determine Why Your App Was Launched.
- 檢查啟動項字典信息。
For apps that aren't scene-based, UIKit loads your default user interface automatically at launch time. Use the application(_:didFinishLaunchingWithOptions:)
method to make additional changes to that interface before it appears onscreen. For example, you might install a different view controller to reflect what the user was doing the last time they used the app.
對於不是基於場景的應用,UIKit在啟動時預設載入用戶界面。使用application(_:didFinishLaunchingWithOptions:)方法來做額外的更改。例如:你可以安裝一個預設控制器來反應用戶上一次使用應用程式做什麼。
Move Long-Running Tasks off the Main Thread
When the user launches your app, make a good impression by launching quickly. UIKit doesn't present your app's interface until after the application(_:didFinishLaunchingWithOptions:)
method returns. Performing long-running tasks in that method or your application(_:willFinishLaunchingWithOptions:)
method might make your app appear sluggish to the user. Returning quickly is also important when launching to the background, because your app’s background execution time is limited.
當用戶啟動應用程式時,通過快速啟動來給用戶留下一個好的印象。UIKit直到應用程式(_:didFinishLaunchingWithOptions:)方法返回後,才會顯示用戶界面,在該方法或(_:willFinishLaunchingWithOptions:)方法中執行耗時操作,可能會讓用戶覺得你的應用程式運行緩慢。在啟動到後臺時,快速返回很重要,因為應用程式在後臺執行時間有限。
Move tasks that are not critical to your app’s initialization out of the launch-time sequence. For example:
將對應用程式啟動不重要的任務移出啟動時間序列。例如:
-
Defer the initialization of features that are not needed immediately.
- 推遲不需要的特性初始化。
-
Move important, long-running tasks off your app’s main thread. For example, run them asynchronously on a global dispatch queue.
- 將重要的長時間運行的任務移出應用程式的主線程。例如:在全局調度隊列中非同步運行他們。
Determine Why Your App Was Launched
When UIKit launches your app, it passes along a launch options dictionary to your application(_:willFinishLaunchingWithOptions:)
and application(_:didFinishLaunchingWithOptions:)
methods with information about why your app was launched. The keys in that dictionary indicate important tasks to perform immediately. For example, they might reflect actions that the user started elsewhere and want to continue in your app. Always check the contents of the launch options dictionary for keys that you expect, and respond appropriately to their presence.
當UIkit啟動你的應用程式時,它會傳遞一個啟動項字典給你的應用程式application(_:willFinishLaunchingWithOptions:)
and application(_:didFinishLaunchingWithOptions:)方法,其中包含了為什麼啟動。字典中的鍵標識立即執行的重要任務。例如:它們可能反應用戶在其他地方啟動的操作,並希望在應用程式中繼續執行。始終檢查啟動項字典的內容,查找您期望的鍵,並對它們做出適當的響應。
Note
For a scene-based app, examine the options passed to the application(_:configurationForConnecting:options:)
method to determine why your scene was created.
對於基於場景的應用程式,檢查傳遞給應用程式(_:configurationforconnected:options:)方法的選項,以確定為什麼創建場景。
Listing 1 shows the app delegate method for an app that handles background location updates. When the location key is present, the app starts location updates immediately, instead of deferring them until later. Starting location updates allows the Core Location framework to deliver the new location event.
清單1顯示了處理後臺位置更新的應用程式的應用程式委托方法。當位置鍵存在時,應用程式立即啟動位置更新,而不是推遲到以後。啟動位置更新允許核心位置框架交付新的位置事件。
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { let locationManager = CLLocationManager() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // If launched because of new location data, // start the visits service right away. if let keys = launchOptions?.keys { if keys.contains(.location) { locationManager.delegate = self locationManager.startMonitoringVisits() } } return true } // other methods… }
The system doesn’t include a key unless your app supports the corresponding feature. For example, the system doesn’t include the remoteNotification
key for an app that doesn’t support remote notifications.
For a list of launch option keys and information about how to handle them, see UIApplication.LaunchOptionsKey
.
除非你的應用程式支持相應的功能,否則系統不會包含密鑰。例如,對於不支持遠程通知的應用程式,系統不包含remoteNotification鍵。
有關啟動選項鍵的列表和有關如何處理它們的信息,請參見UIApplication.LaunchOptionsKey。