UIKIt学习笔记—App and Environment部分(一)

这两天面了牛客网,加上期末考试,所以没有更新,成功拿到offer啦!之后会继续每日更新的!
牛客网的面经在这

今天2021年12月24日,各位平安夜快乐
这一部分是

Responding to the Launch of Your App

每个App都有一个关联的进程,即一个UIApplication对象,App还有一个遵守UIApplicationDelegate协议的app代理对象,用来响应在启动过程中的重要事件。在启动时,UIKit自动创建UIApplication对象和app代理,之后启动主事件周期。

重要提示:iOS14之后启动界面被限制在25MB以下,所以不要用净态图像

下面两个方法处理启动时的初始化代码
application(:willFinishLaunchingWithOptions:)
application(
:didFinishLaunchingWithOptions:)

UIKit calls these methods at the beginning of your app’s launch cycle. Use them to:
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 launches for the first time. For example, install templates or user-modifiable files in a writable directory.
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 launched.

UIKit调用他们做以下操作:

初始化app的数据结构
确定app有运行所需的资源
当app第一次启动时执行所有一次的设置,比如往可写字典中写入模版或用户可更改的文件
链接所有用户需要使用的权限或服务,比如app的消息推送
检查可选启动字典

Use the application(_:didFinishLaunchingWithOptions:) method to make additional changes to that interface before it appears onscreen

用application(_:didFinishLaunchingWithOptions:) 方法实现一些额外的操作,比如做一个VC来反应用户上次用app做了什么

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 the system limits your app’s background execution time
尽量别让application(_:didFinishLaunchingWithOptions:) 方法做一些耗时操作,会堵塞UI,系统还会对后台执行时间做限制。那些不是启动时必要的或者耗时的就放到全局的派发队列里去就行了

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 launched. The keys in that dictionary indicate important tasks to perform immediately. For example, they might reflect actions that the user started elsewhere and wants 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.

Note
For a scene-based app, examine the options that UIKit passes to the application(_:configurationForConnecting:options:) method to determine why it created your scene.

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.
Listing 1 Responding to a location event at launch time

class AppDelegate: UIResponder, UIApplicationDelegate, 
               CLLocationManagerDelegate {
   
    
   let locationManager = CLLocationManager()
   func application(_ application: UIApplication,
              didFinishLaunchingWithOptions launchOptions:
              [UIApplicationLaunchOpt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值