Question

I'm developing an app that uses Core Data for save and retrieve data.

Now I would like to add a badge number on app's icon in the home screen but I have some problems...
I tried this code:

NSInteger section = [self.tableView numberOfSections];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[sectionInfo numberOfObjects];

This code don't work but I don't know how to do this...
What code I must write and where? Inside AppDelegate or RootViewController? And in which method?

Thanks all for the help!

Was it helpful?

Solution

NSInteger section = [self.tableView numberOfSections];

That's not a valid section number; they go from 0 to numberOfSections-1.

OTHER TIPS

NSInteger section = [[self tableView] numberOfSections];

This is going to return back 1 or more. Not a value to be plugging into [[[self fetchedResultsController] sections] objectAtIndex:section]. You should be giving it an actual section (zero or more) not the total number of sections.

In addition you can put a breakpoint on your assignment of the sectionInfo variable and make sure you are not getting a nil back. You can also put a debug after the sectionInfo to make sure you are getting something meaningful back.

If it is nil or returning zero then your badge will not show.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top