Initialize your app’s data structures, prepare your app to run, and respond to any launch-time requests from the system.數據結構
初始化應用程序的數據結構、準備運行應用程序、響應系統啓動時的一些請求。app
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.框架
當用戶點擊桌面的應用程序圖標時,系統就會運行你的應用程序,若是你的應用程序請求特定的事件,系統可能會在後臺進行處理這些事件,而對於一個基於場景的應用程序,當一個場景須要出如今屏幕上或者作一些工做時,系統也會啓動應用程序。less
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對象和委託代理。而後啓動應用程序的主事件循環。async
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.ide
當用戶第一次在設備上啓動你的應用程序時,系統會顯示啓動故事板,直到程序準備好它的UI.顯示故事板的目的是向用戶保證你的應用程序已經啓動,並且正在運行。 若是你的應用程序初始化的速度和準備的過程很是快,那用戶可能只能短暫的看到你的啓動故事板。oop
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項目會自動包含一個默認的啓動故事板,但你能夠根據須要添加更多的故事板,若是想添加新的啓動故事板到項目中,須要作以下操做:ui
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,或者其餘內容。始終建立所需接口的最終版本。不要包含臨時內容或須要本地化的內容。精確地顯示你所提供的,使用你的約束來說你的視圖放入可用的空間。
In iOS 13 and later, always provide a launch storyboard for your app. Don’t use static launch images.
在iOS13及之後,老是提供一個故事板,再也不使用靜態的啓動圖片。
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.
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:)方法來作額外的更改。例如:你能夠安裝一個默認控制器來反應用戶上一次使用應用程序作什麼。
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.
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:)方法,其中包含了爲何啓動。字典中的鍵標識當即執行的重要任務。例如:它們可能反應用戶在其餘地方啓動的操做,並但願在應用程序中繼續執行。始終檢查啓動項字典的內容,查找您指望的鍵,並對它們作出適當的響應。
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。