iOS文档--Responding to the Launch of Your App 响应app的启动

Article

Responding to the Launch of Your App     --响应app的启动

Initialize your app’s data structures, prepare your app to run, and respond to any launch-time requests from the system.

     --初始化您的app的数据结构,准备您的app的运行,并响应来自系统的所有启动期请求。

 

 

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.

       --当用户在主屏幕上点击你的app图标时,系统就会启动你的app。如果你的app请求特定事件,系统可能也会在后台启动你的app来处理这些事件。对于基于scene的app,当某个scene需要在屏幕上显示或scene需要执行某些工作时,系统也会启动app。

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.

       --所有的app都有一个 ”由UIApplication对象表示“ 相关的进程。同时app还有一个遵循UIApplicationDelegate协议的app委托对象,它的工作是响应进程中发生的重要事件。即使是基于scene的app,也使用app delegate来管理基本事件,如启动和终止。在启动时,UIKit会自动创建UIApplication对象和你的app delegate对象。然后,它才启动你的app的主事件循环。

 

 

Provide a Launch Storyboard                      --提供启动的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.

        --当用户第一次在设备上启动你的app时,系统就会显示你的launch storyboard,直到你的app准备好显示它的UI。显示launch storyboard可以向用户保证你的app已经启动了并且正在运行。如果你的app初始化它自己并快速准备它的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项目自动包含一个默认的launch storyboard供您自定义,您可以根据需要添加更多的launch storyboards。如果要将新的launch storyboards 提要添加到项目中,请执行以下操作:

  1. Open your project in Xcode.

  2. Select File > New > New File.

  3. 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.

       --将view添加到launch storyboard中,还有使用“自动布局约束”来调整视图的大小和位置,以便view适应底层环境。例如,您可以创一个闪屏,上面是纯色背景和固定大小的公司logo或其他内容的图像。一定要创建定型的闪屏界面,不要包含临时内容或需要本地化的内容。因为UIKit需要精确地显示你所提供的闪屏,使用你的约束来将你的视图放入可用的空间。

Important

In iOS 13 and later, always provide a launch storyboard for your app. Don’t use static launch images.

        --在iOS 13及更高版本中,始终为app提供启动launch storyboard。不要使用静态的启动图像。

 

 

Initialize Your App's Data Structures                      --初始化app的数据结构

 

Put your app's launch-time initialization code in one or both of the following methods:

       --将app的启动时初始化代码放在以下一个或两个方法中:

UIKit calls these methods at the beginning of your app’s launch cycle. Use them to:

     --UIKit在app启动周期开始时会调用上面的方法。你可以使用它们来做以下操作:

  • Initialize your app's data structures.
     --初始化应用程序的数据结构。

  • Verify that your app has the resources it needs to run.
     --验证你的app是否具有运行所需的资源。

  • 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.
    --执行在app第一次启动时需要所有的一次性设置。例如,在可写目录中安装模板或用户可修改的文件;请参见超链接:执行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.
        --连接app需要使用的所有关键服务。例如,如果您的app支持远程通知,则连接到Apple Push Notification service。

  • Check the launch options dictionary for information about why your app was launched; see Determine Why Your App Was Launched.
         --查看启动选项的字典,了解您的app启动的原因;参考超链接:确定为什么你的应用程序被启动。

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.

       --对于不基于scene的app,UIKit会在启动时自动加载默认用户界面。在该界面出现在屏幕上之前,你可以使用app delegate的application(_: didFinishLaunchingWithOptions:)方法对其进行其他更改。例如,您可以安装不同的vc,以反映用户上次使用app时所做的操作。

 

 

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.

     --当用户启动你的app时,请通过快速启动给用户留下一个好印象。UIKit会等到application(_: didFinishLaunchingWithOptions:)方法返回后才会显示你的app的界面。在该方法或application(_: willFinishLaunchingWithOptions:) 方法中执行长时间运行的任务会让用户觉得您的app运行缓慢。在启动到后台时,快速返回也很重要,因为app的后台执行时间有限。

Move tasks that are not critical to your app’s initialization out of the launch-time sequence. For example:

      --将对app初始化不重要的任务移出启动期序列。例如:

  • 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.
      --将重要的、长时间运行的任务移出app的主线程。例如,可以在全局调度队列上异步运行它们。

 

 

Determine Why Your App Was Launched                   --确定你的app启动为什么要启动

 

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启动您的app时,它会将“启动选项字典”传递给application(_:willFinishLaunchingWithOptions:)方法和application(_:didFinishLaunchingWithOptions:) 方法,并提供有关启动app原因的信息。字典中的key表示要立即执行的重要任务。例如,keys反映了一些可能是用户在其他地方启动的任务,但希望在app中继续执行。始终检查“启动选项字典”的内容,找出你想要的键,并对它们的存在做出适当的响应

Note

For a scene-based app, examine the options passed to the application(_:configurationForConnecting:options:) method to determine why your scene was created.

       --对于基于场景的应用程序,检查传递给application(_:configurationForConnecting: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显示了app delegate在后台“处理位置更新”时方法。当位置的key存在时,app立即启动位置更新,而不是推迟到以后。启动位置更新 “允许Core Location framework框架” 交付新的位置事件。

Listing 1

Responding to a location event at launch time

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.

     --除非你的app支持相应的功能,否则系统不会包含启动选项字典中的key。例如,对于不支持远程通知的应用程序,系统不包含remoteNotification键。

For a list of launch option keys and information about how to handle them, see UIApplication.LaunchOptionsKey.

        --有关启动选项键的列表和有关如何处理它们的信息,请参见超链接:UIApplication.LaunchOptionsKey。

 

Topics                           --专题

Launch Time                     --启动期

 

About the App Launch Sequence                                  --关于app的启动序列

Learn the order in which your custom code is executed at launch time.

 

Performing One-Time Setup for Your App                             --app启动时的一次性设置

Ensure that your app environment is properly configured.

 

Preserving Your App's UI Across Launches                             --在启动期保存app的UI

Return your app to its previous state after it is terminated by the system.

 

See Also

Life Cycle

 

Managing Your App's Life Cycle

Respond to system notifications when your app is in the foreground or background, and handle other significant system-related events.

class UIApplication

The centralized point of control and coordination for apps running in iOS.

protocol UIApplicationDelegate

A set of methods that you use to manage shared behaviors for your app.

 

Scenes

Manage multiple instances of your app’s UI simultaneously, and direct resources to the appropriate instance of your UI.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值