开发者

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

开发者 https://www.devze.com 2023-01-24 05:34 出处:网络
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 eithe

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 ne开发者_Go百科ed 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号