Sök…


Introduktion

AppDelegate är ett protokoll som definierar metoder som kallas av singleton UIA-applikationsobjektet som svar på viktiga händelser under en apps livstid.

Normalt används för att utföra uppgifter vid programstart (installationsappmiljö, analitetskoder (ex .: Mixpanel / GoogleAnalytics / Crashlitics), DB-stack etc.) och avstängning (ex .: spara DB-sammanhang), hantering av URL-öppna förfrågningar och liknande applikationsomfattande uppgifter.

Alla tillståndsapplikationer med AppDelegate-metoder

För att få uppdatering eller att göra något innan appen går live till användaren kan du använda metoden nedan.

AppDidFinishLaunching

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

Medan app anges i förgrunden:

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

Vid applansering och även bakgrund till förgrunds hitmetoden:

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

Medan App Ange i bakgrunden:

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

Medan app avgå aktiv

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

Medan appen avslutas:

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

AppDelegate roller:

  • AppDelegate innehåller din startup code .
  • Den svarar på key changes i state för din app. Specifikt svarar det både på tillfälliga avbrott och på förändringar i körningstillståndet för din app, till exempel när din app övergår från förgrunden till bakgrunden.
  • Den responds to notifications ursprung utanför appen, till exempel fjärrmeddelanden (även känd som push-meddelanden), varningar med lågt minne, aviseringar om nedladdning och mer.
  • Den determines om state preservation och restoration ska ske och hjälper till att bevara och återställa processen vid behov.
  • Den responds to events som riktar sig till appen själv och är inte specifika för din apps åsikter eller visningskontrollanter. Du kan använda den för att lagra din apps centrala dataobjekt eller något innehåll som inte har en ägare för visningskontroller.

Öppna en URL-specifik resurs

Ber delegaten att öppna en resurs som anges med en URL och tillhandahåller en ordlista med lanseringsalternativ.

Exempel på användning:

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

Hantera lokala och fjärrmeddelanden

Exempel på användning:

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

Registrering:

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

Hantering av fjärrmeddelanden:

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

Lokala aviseringar:

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

Hanteringsåtgärd (utgått):

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
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow