【iOS翻译】App启动时的响应过程

 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 UIApplicationDelegateprotocol—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项目会自动包含一个默认的启动故事板,但你可以根据需要添加更多的故事板,如果想添加新的启动故事板到项目中,需要做如下操作:

  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.

  添加视图到启动故事板中,使用自动布局约束来调整视图的大小和位置,以适应底层的环境。例如:你可以创建一个纯色的背景和固定大小的图片来标识你的公司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。

原文地址:https://www.cnblogs.com/xjf125/p/11961693.html

时间: 2024-10-10 20:35:47

【iOS翻译】App启动时的响应过程的相关文章

ios替换app启动图片时系统报错的解决办法

ios替换app启动图片时系统报错的解决办法:我个人建议是在开发时候经常行的保存项目,并且在修改项目图标图片.app启动图片前,一定要先备份一份没有添加这两项图片的项目. 如果您的项目已经开发完成了,进入到发布前添加项目图标.app启动图片的时候,一定要确定了这两项的所有图片不会更改了再去添加,否则更改已经添加好的加项目图标.app启动图片就会报错, 如果您报错了,百度之后也没有解决办法,那么就尝试在已经备份的项目中重新去添加图片就可以了.

Android App 启动时显示正在加载图片(源码)

微信.QQ.天天动听等程序,在打开时显示了一张图片,然后跳转到相关界面.本文实现这个功能,其实很简单.... 新建两个Activity,LoadingActivity,MainActivity,将LoadingActivity设置为android.intent.action.MAIN.使用TimerTesk,或者Thread将LoadingActivity显示几秒后跳转到MainActivity界面. LoadingActivity: new Timer().schedule(new Timer

loadView在App启动时到底都干了些什么?

loadView在App启动时到底都干了些什么? 查阅苹果官方文档如下: 1. 当你访问一个ViewController的view属性时,如果此时view的值是nil,那么,ViewController就会自动调用loadView这个方法.这个方法就会加载nib文件或者创建一个空的view对象(self.view = nil). 2.使用nib文件创建view时,没必要重载loadView方法,因为loadView的作用就是加载nib.如果你非要重载,那么必须调用[super loadView]

iOS APP启动时所有方法的调用顺序分析

一个应用程序的启动过程要包括代理的创建,控制器的加载和控制器view的加载,这其中有很多关于生命周期的方法,每个方法都是有先后顺序的,如果调用顺序拿不准,或者某段代码写的方法不恰当,就会遇到各种奇葩问题.本文不怕麻烦的在几乎所有启动时要调用的方法里都用了 __FUNCTION__ 打印.结果还有有些地方出人意料的 首先回顾一下应用程序的启动过程 ①.先加载Main函数 ②.在Main函数里的 UIApplicationMain方法中,创建Application对象 创建Application的D

【Android端 APP 启动时长获取】启动时长获取方案及具体实施

一.什么是启动时长? 1.启动时长一般包括三种场景,分别是:新装包的首次启动时长,冷启动时长.热启动时长 冷启动 和 热启动 : (1)冷启动:当启动应用时,后台没有该程序的进程,此时启动的话系统会分配一个新的进程给应用. (2)热启动:程序的进程依然存在,启动时通过已有进程启动进入到Activity显示页面的,就是热启动,或者从Android官网来看我们获取到的其实是温启动时长,就是Activity不存在的情况. (3)新装包的启动时长: 新装包的启动时长,预估是最长的,并且在5.0以下及5.

iOS中app启动闪退的原因

这种情况应和所谓的内存不足关系不大,很少有程序会在初始化时载入大量内容导致崩溃,并且这类问题也很容易在开发阶段被发现,所以内存不足造成秒退的可能性低(内存不足退,通常是程序用了一段时间,切换了几个画面以后发生的). 而且秒退是发生在程序刚刚启动的时候,在开发.苹果审核阶段都没有被发现的最大可能性就是,这个问题只会发生在老版系统.老版机型上. 对于很多开发者(尤其是个人开发者),进行所有 iOS 版本,所有 iOS 机型覆盖测试是有难度的,苹果审核时也只是重点审核该应用在新机器.新版本下的运行情况

APP启动时白屏优化及multidex优化

参考 https://juejin.im/post/5d95f4a4f265da5b8f10714b https://blog.csdn.net/suyimin2010/article/details/80635579 https://www.cnblogs.com/whycxb/p/9312914.html 问题说明 当打开一个Activity时,如果这个Activity所属Application还没有在运行,系统会为这个Activity的创建一个进程(每开启一个进程都会有一个Applicat

MTK-Android APP启动时瞬间闪现黑屏(Theme & Style)

闪屏原因:主要是我们启动Activity的时候,需要跑完onCreate和onResume:Android系统需要处理一些数据后,才会显示.按照这种思路,是不是我把初始化的工作尽量减少就可以避免黑屏?事实是,就算你onCreate啥都不做,仍然会闪一下黑屏,因为初始化解析界面时需要一定时间,下面是解决办法: 1.自定义Theme设置背景图Theme<style name="Theme.AppStartLoad" parent="android:Theme"&g

iOS 应用程序启动时要做什么

当您的应用程序启动(无论是在前台或后台),使用您的应用程序委托application:willFinishLaunchingWithOptions:和application:didFinishLaunchingWithOptions:方法来执行以下操作: 检查启动选项字典的内容,了解有关启动应用程序的原因,并做出相应的响应. 初始化您的应用程序的关键数据结构. 准备您的应用程序的窗口和视图以供显示: 在启动时,系统会自动加载您的应用程序的main Storyboard加载初始视图控制器. app