수색…


소개

AppDelegate 는 앱 수명 기간 중 중요한 이벤트에 대한 응답으로 싱글 톤 UIApplication 객체에 의해 호출되는 메소드를 정의하는 프로토콜입니다.

일반적으로 응용 프로그램 시작 (설치 응용 프로그램 환경, analitycs (예 : Mixpanel / GoogleAnalytics / Crashlitics), DB 스택 등) 및 종료 (예 : DB 컨텍스트 저장), URL 열기 요청 및 유사한 응용 프로그램 전반의 작업을 수행하는 데 사용됩니다. 작업.

AppDelegate 메서드를 통한 모든 응용 프로그램 상태

앱 업데이트가 시작되기 전에 업데이트를 받거나 작업을 수행하려면 아래 방법을 사용할 수 있습니다.

AppDidFinishLaunching

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Write your code before app launch
    return YES;
}

포어 그라운드에서 앱을 입력하는 동안 :

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

앱 실행과 포 그라운드 배경이 메소드 아래에 나타 났을 때 :

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

앱이 백그라운드에서 들어가는 동안 :

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

앱이 사임하는 동안 활성

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

앱이 종료되는 동안 :

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

AppDelegate 역할 :

  • AppDelegate는 앱의 startup code 포함합니다.
  • statekey changes 에 응답합니다. 특히, 앱이 포 그라운드에서 백그라운드로 전환 될 때와 같이 일시적인 중단과 앱 실행 상태의 변경에 모두 응답합니다.
  • 원격 알림 (푸시 알림이라고도 함), 메모리 부족 경고, 다운로드 완료 알림 등 응용 프로그램 외부에서 발생한 responds to notifications .
  • state preservationrestoration 이루어져야하는지 여부를 determines 하고 필요에 따라 보존 및 복원 프로세스를 지원합니다.
  • 앱 자체를 타겟팅하고 앱의보기 또는보기 컨트롤러와 관련이없는 responds to events . 앱의 중앙 데이터 개체 또는 소유 한보기 컨트롤러가없는 콘텐츠를 저장하는 데 사용할 수 있습니다.

URL 지정 리소스 열기

대리인에게 URL로 지정된 리소스를 열도록 요청하고 시작 옵션 사전을 제공합니다.

사용 예 :

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return SomeManager.shared.handle(
            url,
            sourceApplication: options[.sourceApplication] as? String,
            annotation: options[.annotation]
        )
    }

로컬 및 원격 알림 처리

사용 예 :

/* Instance of your custom APNs/local notification manager */  
private var pushManager: AppleNotificationManager!

기재:

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    // Called to tell the delegate the types of notifications that can be used to get the user’s attention
    pushManager.didRegisterSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    // Tells the delegate that the app successfully registered with Apple Push Notification service (APNs)
    pushManager.didRegisterDeviceToken(deviceToken)
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    // Sent to the delegate when Apple Push Notification service cannot successfully complete the registration process.
    pushManager.didFailToRegisterDeviceToken(error)
}

원격 알림 처리 :

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    // Remote notification arrived, there is data to be fetched
    // Handling it
    pushManager.handleNotification(userInfo,
                                   background: application.applicationState == .Background
    )
}

로컬 알림 처리 :

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    pushManager.handleLocalNotification(notification, background: false)
}

처리 작업 (사용 중지됨) :

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject],
                     completionHandler: () -> Void) {
    pushManager.handleInteractiveRemoteNotification(userInfo, actionIdentifier: identifier, completion: completionHandler)
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow