iOS개발/Swift 기본

iOS 13 미만 AppDelegate / SceneDelegate 설정

Larooly 2024. 6. 27. 10:10

안녕하세요. 

최근에 어떤 버그를 찾느라 iOS 12 를 개발할 일이 잠깐 있었는데

iOS 13 미만에서는 따로 설정을 해야하는게 있더라고요 

(최소가 12.0 이니까 사실상 iOS 12를 위한 코드라 생각하시면 됩니다.)

 

간단하게 프로젝트 하나의 최소 지원 버전을 최소인 12.0 으로 하면  

오류 발생

'UIScene' is only available in iOS 13.0 or newer

이 비슷한 말이 한 9개 정도? AppDelegate / SceneDelegate 에 뜨게 됩니다.

 

오류 원인 

- 말 그대로 해당 기능은 iOS 13 부터 사용이 가능합니다. 

- 또한 SceneDelegate는 iOS 13 부터 사용했기때문에 12에서는 사용이 불가합니다.

- 그래서 해당 코드를 버전에 따라 분리하는게 필요합니다. 

- 최소 지원을 올리는 것도 방법이지만 지원을 해야하는 경우 참고합시다. 

 

해결 방법

https://stackoverflow.com/questions/58405393/appdelegate-and-scenedelegate-when-supporting-ios-12-and-13

 

AppDelegate and SceneDelegate when supporting iOS 12 and 13

I need to support iOS 12 and iOS 13. Should I be duplicating code between AppDelegate and SceneDelegate? For example: func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options

stackoverflow.com

여기 정답이 나와 있습니다. 

한번 같이 따라해봅시다.

 

SceneDelegate 로 이동하셔서 아래 처럼 한줄을 추가해주세요. 

@available(iOS 13.0, *) // <- 이거
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

AppDelegate 로 이동한 후 아래 처럼 바꿔주세요 

(헷갈리실까봐 이건 통으로 올릴께요.)

class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow? //<- 추가

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if #available(iOS 13.0, *) {//<- 추가
                    // Do nothing. SceneDelegate will handle the UI setup.
        } else {//<- 아래 통으로 추가
            window = UIWindow(frame: UIScreen.main.bounds)
            let rootViewController = UIViewController()
            // 루트 뷰 컨트롤러 설정
            window?.rootViewController = rootViewController
            window?.makeKeyAndVisible()
        }
        return true
    }
    // MARK: UISceneSession Lifecycle
    @available(iOS 13.0, *)//<- 추가
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    @available(iOS 13.0, *)//<- 추가
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // 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.
    }
}

 

첫번째 함수(didFinishLaunchingWithOptions)를 제외하고는 아래 코드를 함수위에 추가한 것뿐입니다.

@available(iOS 13.0, *)//<- 추가

 

* 참고로 didFinishLaunchingWithOptions 에 위 코드 추가안하시면 iOS 12 는 검은화면만 보입니다.

* 그걸 방지하기위해 코드가 몇개 추가된 것 뿐입니다.

 

이렇게 하시면 iOS 12에서도 정상적으로 빌드가 가능하게 됩니다. 

그럼 오늘도 파이팅입니다.