Question

Je dois savoir s'il est possible de créer un nouveau thread pour gérer les notifications locales réglage.

Mon application dépend fortement de ces notifications, donc je veux faire le travail d'application alors que le téléphone fixe les notifications.

Exemple:

(maintenant)

vous lancez l'application, les blocages d'applications à l'écran de démarrage pour définir les notifications locales, puis il lance.

(Je veux)

Les applications et les lancements sont utilisables alors que les notifications locales sont définies.

J'ai besoin quelques exemples de code, aussi, s'il vous plaît:)

(pour l'enregistrement, je suis en train de 60 notifications locales à chaque fois que l'application entre premier plan pour mes propres raisons ...)

Merci !!

Était-ce utile?

La solution

Oui, cela peut être fait, je le fais tout le temps:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Add the navigation controller's view to the window and display.
    [NSThread detachNewThreadSelector:@selector(scheduleLocalNotifications) toTarget:self withObject:nil];
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

-(void) scheduleLocalNotifications
{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    for (int i = 0; i < 60; i++)
    {
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;
        NSDate *sleepDate = [[NSDate date] dateByAddingTimeInterval:i * 60];
        NSLog(@"Sleepdate is: %@", sleepDate);

        localNotif.fireDate = sleepDate;    

        NSLog(@"fireDate is %@",localNotif.fireDate);
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"This is local notification %i"), i];

        localNotif.alertAction = NSLocalizedString(@"View Details", nil);
        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
        NSLog(@"scheduledLocalNotifications are %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
        [localNotif release];

    }

    [pool release];
}

Tiré d'un projet sur lequel je travaille maintenant, je peux confirmer que cela fonctionne comme prévu.

EDIT: Exemple a été répandu dans scheduleLocalNotifications parce que la manipulation du NSAutoreleasePool avait disparu -. Maintenant il est ajouté à l'exemple

Autres conseils

Une façon de faire est avec des fils est avec performSelectorInBackground.

Par exemple:

[myObj performSelectorInBackground:@selector(doSomething) withObject:nil];

Vous devez noter, cependant, que Apple est assez fortement recommande que vous utilisez des concepts de niveau supérieur comme NSOperations et Queues Dispatch au lieu de fils fraye explicitement. Voir Concurrency Guide de programmation

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top