Question

I am making an age calculation app, and I wanted to connect the number of days, which is a label inside the app, to an application badge (the little red shape on an app icon, eg. Mail(when you have mail)). Is there any way to do this?

All help is appreciated! Thanks

Was it helpful?

Solution

The app icon badge can be set by:

[UIApplication sharedApplication].applicationIconBadgeNumber = 42; //Number of Days

For example, if your label said just a number, you could use this code:

NSInteger number = self.myLabel.text.integerValue;
[UIApplication sharedApplication].iconBadgeNumber = number;

OTHER TIPS

If I understand correctly, you want to synchronize the value of the icon badge with the value of the label:

// Add an observer that listens for changes in the text of the label
[label addObserver:self
        forKeyPath:@"text"
           options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
           context:NULL];

// Implement the observer method on `self`
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSString *text = [change objectForKey:NSKeyValueChangeNewKey];
    [UIApplication sharedApplication].applicationIconBadgeNumber = text.integerValue;
}

Use this code:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:YOUR_VALUE];

to change the value of the Icon on the Badge

NSInteger badgeNumber = [[yourLabel text] integerValue];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeNumber];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top