ios13多窗口(UIWindowScene)学习笔记

ios13引入了UIWindowScene类、UIWindowSceneDelegate协议以便支持多窗口功能,但其适用于ipad,不适用于iphone,因为iphone不支持多窗口功能。注意,这里说的窗口不是UIWindow,而是UIWindowScene。

ios13前后的app的UI架构变化

ios13之前的app的UI架构

ios13及以后的app的UI架构

由上图可知,UIWindowScene和UIWindow是一对多的关系。

下图所示的是UIWindowScene的定义,它用于管理1个或者多个UIWindow。

第一个app demo

如下面两张图所示,当我们在xcode11及以后的xcode版本中创建一个ios app工程时,xcode会自动为我们创建"一个app运行起来所需"的文件。当创建一个新的app工程时,相比于xcode10及其之前的版本,xcode11之后会自动为我们新增3处内容(如下图的红框所示):①SceneDelegate类;②info.plist里面的Application Scene Manifest配置选项;③AppDelegate类里面的

  • application:configurationForConnectingSceneSession:options:方法和
  • application:didDiscardSceneSessions:方法。

SceneDelegate类

我们先来看看SceneDelegate类里面的内容,如下代码所示。SceneDelegate有个window属性,说明ios13之后的window由SceneDelegate管理。当看到方法里面的"DidBecomeActive"、"WillResignActive"、"WillEnterForeground"、"DidEnterBackground"时,我们便可以猜测出这是app进入和退出前后台时的回调。

objectivec 复制代码
#import <UIKit/UIKit.h>
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
 
@property (strong, nonatomic) UIWindow * window;
 
@end
 
@interface SceneDelegate ()
@end
 
@implementation SceneDelegate
 
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
}
 
- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
 
- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
 
- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}
 
- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}
 
- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}
 
@end

info.plist里面的Application Scene Manifest配置选项

如下第1张图所示,当我们给SceneDelegate类的方法添加断点,然后运行app时,发现断点生效了。那么,SceneDelegate类是在哪初始化的呢?如下第2张图所示,如果你在工程中搜索"SceneDelegate"关键字,就会发现并没有任何代码来创建SceneDelegate实例,此时你可以看到info.plist文件里面有关于SceneDelegate的配置项。我们便可以猜测出SceneDelegate实例是系统默认帮我们创建的。

第1张图


第2张图

app demo运行

当我们在ipad上运行前面刚刚创建的app时,结果如下图所示,发现屏幕的顶部中间多了个3点按钮(下图的红框所示)。你可以点击3点按钮来使用里面的功能(请看下面的录屏)。

录屏demo链接

UIApplication、UIApplicationDelegate、UIWindowScene、UIWindowSceneDelegate、UISceneSession的职责

xcode11版本之后的xcode创建的ios app如何适配ios13以下的系统

  1. 在AppDelegate.h中添加window属性。
objectivec 复制代码
#import <UIKit/UIKit.h>
 
@interface AppDelegate : UIResponder <UIApplicationDelegate>
 
@property (strong, nonatomic) UIWindow * window;
 
@end
  1. 在AppDelegate.m中初始化ios13系统以下的情况下的window。
objectivec 复制代码
#import "AppDelegate.h"
#import "ViewController.h"
 
@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if (@available(iOS 13.0, *)) {
        
    } else {
        //适配ios13以下的系统
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
        ViewController *vc = [ViewController new];
        self.window.rootViewController = vc;
        [self.window makeKeyAndVisible];
    }
    return YES;
}
 
#pragma mark - UISceneSession lifecycle
 
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
 
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
}
 
@end
  1. 在SceneDelegate.m里面初始化window。
objectivec 复制代码
#import "SceneDelegate.h"
#import "ViewController.h"
 
@implementation SceneDelegate
 
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    if (@available(iOS 13.0, *)) {
        UIWindowScene *windowScene = (UIWindowScene *)scene;
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
        [self.window setWindowScene:windowScene];
        ViewController *vc = [ViewController new];
        UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
        self.window.rootViewController = nvc;
        [self.window makeKeyAndVisible];
    }
}
 
@end
  1. 运行app,结果如下图。
  2. 我们让app支持多窗口功能,然后运行app,接着添加窗口。
  3. 此时看看app的UI层级图,发现此时一个有3个窗口(UIWindowScene)。

ios13后的AppDelegate的职责发生了变化

ios13之前

如下图,ios13之前的Appdelegate职责有:1 进程生命周期相关的回调(比如application:didFinishLaunchingWithOptions:方法); 2 UI的生命周期相关的回调(比如applicationDidBecomeActive:方法)。

ios13之后

如下图,ios13之后,AppDelegate负责进程生命周期相关和UISceneSession生命周期相关的回调。而UI的生命周期相关的回调则由UISceneDelegate来负责。

相关推荐
Komorebi.py1 分钟前
【Linux】-学习笔记05
linux·笔记·学习
亦枫Leonlew10 分钟前
微积分复习笔记 Calculus Volume 1 - 6.5 Physical Applications
笔记·数学·微积分
冰帝海岸5 小时前
01-spring security认证笔记
java·笔记·spring
小二·6 小时前
java基础面试题笔记(基础篇)
java·笔记·python
朝九晚五ฺ8 小时前
【Linux探索学习】第十四弹——进程优先级:深入理解操作系统中的进程优先级
linux·运维·学习
wusong9999 小时前
mongoDB回顾笔记(一)
数据库·笔记·mongodb
猫爪笔记9 小时前
前端:HTML (学习笔记)【1】
前端·笔记·学习·html
Resurgence039 小时前
【计组笔记】习题
笔记
pq113_610 小时前
ftdi_sio应用学习笔记 3 - GPIO
笔记·学习·ftdi_sio
澄澈i10 小时前
设计模式学习[8]---原型模式
学习·设计模式·原型模式