在XCode 11后的版本上开发iOS版本低于13的工程(Objective-C)

由于在XCode11版本后,苹果添加了SwiftUI的界面布局功能,它会在创建的工程中生成SceneDelegate.h和SceneDelegate.m文件。如果我们将项目的版本设置低于iOS 13,在编译项目时将会报错,提示UIScene类只能在iOS 13及以上版本上使用等错误。为了达到降低目标版本的目的,需要修改以下几个方面的代码:

  1. 布局方法修改
    在创建工程时,选择使用Storyboard的布局方法。


    image.png
  2. 修改SceneDelegate.m类
    因为该类只在iOS 13后使用,可以使用API_AVAILABLE(ios(13.0)) 宏将其方法标记为iOS 13可用。修改代码如下:

#import "SceneDelegate.h"

@interface SceneDelegate ()

@end

@implementation SceneDelegate

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions API_AVAILABLE(ios(13.0)) {
    // 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  API_AVAILABLE(ios(13.0)) {
    // Called as the sc API_AVAILABLE(ios(13.0))ene 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 API_AVAILABLE(ios(13.0)) {
    // 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 API_AVAILABLE(ios(13.0)) {
    // 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 API_AVAILABLE(ios(13.0)) {
    // 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 API_AVAILABLE(ios(13.0)) {
    // 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
  1. 还需要修改AppDelegate.h类
    因为iOS 13版本的设备会走SceneDelegate类,而低于iOS 13的设备将会走该类,因此需要在该类中添加window变量,不然应用启动后将会是黑屏。代码修改如下:
#import 

@interface AppDelegate : UIResponder 

@property(nonatomic, strong) UIWindow *window;

@end

由于自动生成的AppDelegate.m类中还有方法使用到了高版本的类,因此需要将其标注为指定版本后使用。AppDelegate.m的完整代码如下:

#import "AppDelegate.h"
#import "MainViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return YES;
}

#pragma mark - UISceneSession lifecycle

- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) {
    // 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];
}

- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet *)sceneSessions API_AVAILABLE(ios(13.0)) {
    // 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.
}

@end

注意:如果需要使用代码指定第一个进入的界面需要在SceneDelegate.swift和AppDelegate.swift都指定。

如果该文章对你有用,麻烦给我个赞, 谢谢!

你可能感兴趣的:(在XCode 11后的版本上开发iOS版本低于13的工程(Objective-C))