Painted levels: being fostered fostered fostered
“IOS13” “Xcode11” “SceneDelegate” by dac_1033: review: QiShare team
By default, Xcode 11 new projects will create applications that manage multiple UIWindows through UIScene. In addition to the AppDelegate, there will be a SceneDelegate in the project to implement the result of iPadOS supporting multiple Windows. H no longer has the window property, the window property is now defined in scenedelegate. h, the AppDelegate has a new delegate method for the scene, and SceneDelegate has a corresponding delegate method. So, when we’re developing for different versions of iOS with Xcode11, we need to do some adaptation.
After creating a project, you can see that the relevant Xcode development interface looks like this:
Appdelegate.h has two more default proxy methods:
#pragma mark - UISceneSession lifecycle /* 1. This method is required if you do not include scene configuration data in your APP's info.plist file, or if you want to dynamically change scene configuration data. UIKit will call this method before creating a new scene. 2. Method returns a UISceneConfiguration object that contains the details of the scenario, including the type of scenario to create, the delegate object to manage the scenario, and the scenario feed that contains the initial view controller to display. If this approach is not implemented, the scenario configuration data must be provided in the info.plist file of the application. To summarize: it is configured in info.plist by default, so it is ok not to implement this method. If not configured then you'll need to implement this method and return a UISceneConfiguration object. In the Configuration parameters, the Application Session Role is an array, and each item has three parameters: Configuration Name: Indicates the Name of the current Configuration. Delegate Class Name: Which Scene proxy object is associated with. StoryBoard name: Which StoryBoard the Scene uses. Note: the SceneDelegate class will be called automatically if the Scene named Default Configuration is called in the proxy method. SceneDelegate is associated with the AppDelegate. */ - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role]; } // call back when closing one or more scenes in split screen - (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. }Copy the code
The default proxy method in scenedelegate. m is as follows:
- (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 neccessarily 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.
}
Copy the code
1. Multiple Windows is not required
If you need to support iOS 13 and previous versions of iOS, and you do not need the function of multiple Windows, you can delete the configuration data of Application Scene Manifest in the project info.plist file. The AppDelegate delegate method for Scene and the SceneDelegate class can be deleted. If you use pure code to display the interface, you need to manually add the Window property in appdelegate.h.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];
ViewController *con = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
Copy the code
2. Supports multiple Windows
The Application Scene Manifest configuration item in the iOS 13 project info.plist is available for the iPad Multiple Windows feature. When the Application Scene Manifest configuration item is left undeleted (whether the project supports multiple Windows is a checkable item), the Lifecycle method of the AppDelegate is no longer available. You need to use the lifecycle method provided by UIScene in SceneDelegate. And you’re going to do two sets of Settings in the Scene for iOS 13 and two sets of Settings in the AppDelegate for iOS 13. Here is the code to implement the interface display in pure code:
/ / / / AppDelegate. M - (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {// Override point for Customization after application launch.if (@available(iOS 13.0, *)) { } else { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.window setBackgroundColor:[UIColor whiteColor]]; ViewController *con = [[ViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con]; [self.window setRootViewController:nav]; [self.window makeKeyAndVisible]; } return YES; }Copy the code
//// scenedelegate. m - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session Options :(UISceneConnectionOptions *)connectionOptions {// create a new window if (@available(iOS 13.0, *)) { UIWindowScene *windowScene = (UIWindowScene *)scene; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.window setWindowScene:windowScene]; [self.window setBackgroundColor:[UIColor whiteColor]]; ViewController *con = [[ViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con]; [self.window setRootViewController:nav]; [self.window makeKeyAndVisible]; }}Copy the code
Note: If storyboard is not used, you need to remove the storyboard item from the configuration:
Recommended articles:
IOS WKWebView basic usage Swift 5.1 (4) – Collection type iOS IOS13 DarkMode adaptation iOS13 DarkMode adaptation iOS13 DarkMode adaptation iOS13 DarkMode adaptation iOS13 DarkMode adaptation iOS13 DarkMode adaptation iOS13 DarkMode adaptation