サーチ…


前書き

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.
}

Appがバックグラウンドで入力している間:

- (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:.
}

AppDelgateロール:

  • AppDelegateには、アプリのstartup codeが含まれています。
  • これは、あなたのアプリのstate key changesに対応しkey changes 。具体的には、アプリケーションがフォアグラウンドからバックグラウンドに移行するなど、一時的な中断とアプリケーションの実行状態の変化の両方に応答します。
  • これresponds to notifications 、リモート通知(プッシュ通知とも呼ばれます)、メモリ不足警告、ダウンロード完了通知など、アプリ外から発信されresponds to notificationsresponds to notificationsます。
  • state preservationrestorationが行われるべきかどうかをdeterminesし、必要に応じて保全と復旧のプロセスを支援する。
  • これresponds to events 、アプリ自体をターゲットとし、アプリのビューやビューコントローラに固有ではないresponds to eventsresponds 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