Programming/Swift(iOS)

[iOS 교육]

홍열 2023. 5. 4. 10:05
728x90

 

https://hongprogrammer.tistory.com/210

 

[iOS교육] XCode가 생성한 파일 삭제 방법

XCode가 자동 생성한 파일을 제거하는 방법 1) 자동 생성된 파일 제거 .swift 파일 3개 AppDelegate, SeceneDelegate, ViewController storyboard 파일 2개 Main, LaunchScreen 2) info에서 "Application Scene Manifest" 제거 3) 프로

hongprogrammer.tistory.com

파일 삭제한 순수 상태에서 Code로 iOS Programming 진행

 

iOS 프로그램이 지켜야되는 규칙

UIApplicationDelegate 프로토콜을 채택한 클랙스를 만들어야한다

-> 관례적으로 AppDelegate 클래스 사용

//
//  AppDelegate.swift
//  edu_ios_1
//
//  Created by hy on 2023/05/04.
//

import Foundation
import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate
{
    
}

 

main.swift에서

  • UIApplicationMain() 함수 호출
  • 4번째 인자로 AppDelegate에서 만든 클래스 이름을 전달
//
//  main.swift
//  edu_ios_1
//
//  Created by hy on 2023/05/04.
//

import Foundation
import UIKit

let argc = CommandLine.argc
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to: UnsafeMutablePointer<Int8>?.self, capacity: Int(CommandLine.argc))

UIApplicationMain(argc, argv, nil, NSStringFromClass(AppDelegate.self))

 

왜 이 code를 만들었는가

 

error 발생 -> OS -> Event Queue -> Application Object(UIApplication 타입의 객체), main run loop

-> AppDelegate Object(App의 상태 변화에 대응)

UIApplicationMain() 함수

  • UIApplication 타입의 객체를 생성해서 main run loop 수행
  • 마지막 인자로 전달된 클래스의 객체를 생성해서 Application Object에 등록
  • 발생된 event를 AppDelegate객체에 전달(App 상태 변화와 관련된 이벤트)

AppDelegate에서 여러가지 일을 할 수 있음

  • 앱이 실행될때, 앱이 종료될때 등등..여러 이벤트에 대한 대응 가능
class AppDelegate: UIResponder, UIApplicationDelegate
{
    // 앱이 실행될때 찍히게 해놓는다
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("Launch App")
        return true
    }
}

 

@main

  • Applicationd의 진입점 클래스를 지정
  • main.swift를 만들 필요가 없다.
  • 예전에는 @UIApplicationMain을 사용