简体   繁体   中英

Not the NSTimer or the applicationSignificantTimeChange then what do I use?

I want text to change once a day even if my app is not open at the time NSTimer doesn't run while your app is closed, applicationSignificantTimeChange won't get the message if my app is closed either.

What I need to do is.

(1) get the current date (2) choose a phrase based on the current date and (3) update your label

I still might use NSTimer or applicationSignificantTimeChange to handle the case where my app is open at midnight, but I need to get the phrase-picking method working first so my timer or time change method can call it at midnight for the new date

Can anyone help me out with this problem and what I need to do to make it work?

I hope this code is good enough to stop all the similar questions you are asking way too often.
Yes, lesson for the future: bug me with a lot of questions in categories I'm interested in and I will deliver the code.

- (void)updateLabelForDate:(NSDate *)date {
    NSTimeInterval timeInterval = [date timeIntervalSinceReferenceDate];
    NSInteger days = timeInterval / (60*60*24);
    NSArray *sloganArray = [NSArray arrayWithObjects:
                            NSLocalizedString(@"Slogan for day 1", nil),
                            NSLocalizedString(@"Slogan for day 2", nil),
                            NSLocalizedString(@"Slogan for day 3", nil),
                            NSLocalizedString(@"I'll hope you'll get it", nil),
                            nil];
    NSInteger usedSloganIndex = (int)days % [sloganArray count];
    NSString *slogan = [sloganArray objectAtIndex:usedSloganIndex];
    NSLog(@"Slogan: %@", slogan);
}

- (void)applicationSignificantTimeChange:(UIApplication *)application {
    [self updateLabelForDate:[NSDate date]];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];
    [self updateLabelForDate:[NSDate date]];
    // the following is there to prove that this code works.
    NSDate *date = [NSDate date];
    for (int i = 0; i < 10; i++) {
        NSLog(@"Date: %@", date);
        [self updateLabelForDate:date];
        date = [date dateByAddingTimeInterval:(60*60*24)];
    }
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [self updateLabelForDate:[NSDate date]];
}

You can't, if the application isn't running.

You can however use a "Local Notification" - which is sort of like a "Push Notification" - except it is sent by your own device - to tell you that the app needs attention. This can be scheduled.

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM