views:

1215

answers:

2

Hello there! I'm tryin to use some code to create a badge number for the iPhone App Icon.

I can successfully do this, however the code I am using (below) is pretty bad and could easily be done better, however I do not have the logic knowledge to be able to do this.

This is what it's doing...

  1. Get's the 'ID' of a user from the first message in a UITableView. It then checks this against the stored ID (from the first object when it was last loaded) and makes the badge number 1.
  2. Then checks the second ID in a message to see if that is the same as the stored. If not it will make the badge number 2.
  3. etc etc..

What I want it to do is automatically check the IDs and add '1' to the badge number until the first post with the correct ID is found however, I'm not too sure on the coding.

Any help would be great!

Thanks

(The code is below:)

     NSDictionary* tweetValues = [[NSDictionary alloc] init];
 tweetValues = [tweets objectAtIndex:1];
 NSString *newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
 NSString *oldStatusID = [[NSUserDefaults standardUserDefaults] stringForKey:@"latestID"];
 [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
 if([newStatusID isEqualToString:oldStatusID]){
  [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
 } else {
  tweetValues = [tweets objectAtIndex:2];
  newStatusID = [[tweetValues objectForKey:@"id"] stringValue];

  if ([newStatusID isEqualToString:oldStatusID]){
   [UIApplication sharedApplication].applicationIconBadgeNumber = 1;
  }else{

   tweetValues = [tweets objectAtIndex:3];
   newStatusID = [[tweetValues objectForKey:@"id"] stringValue];

   if ([newStatusID isEqualToString:oldStatusID]){
    [UIApplication sharedApplication].applicationIconBadgeNumber = 2;
   }else{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 3;
   }
  }

 }
A: 

I'm not exactly sure what you are trying to, but looking at your code I think you need to use a for loop.

Also the first line of your code is leaks. I'm not sure what you are trying to do. As far as I can see you don't have to create an empty dictionary here.

The line where you retrieve your tweetValues (tweetValues = [tweets objectAtIndex:1];) is also strange. You are retrieving the object at index 1. Since arrays are zero-indexed you are retrieving the second object in the array. If you wanted to retrieve the first you need to call: [tweets objectAtIndex:0];

If I had to refactor your code I would probably do something like:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

NSString *oldStatusID = [[NSUserDefaults standardUserDefaults] stringForKey:@"latestID"];
for (NSDictionary *tweetValues in tweets) { // Iterate over all the tweetValues in tweets (assume tweets exist?)

 NSString *newStatusID = [[tweetValues objectForKey:@"id"] stringValue];

 if ([oldStatusID isEqualToString:newStatusID]) {

  [UIApplication sharedApplication].applicationIconBadgeNumber++; // Increase the badge number by 1
 }
}

I hope I correctly interpreted your code. If not, some more context might be useful.

klaaspieter
Hey there, what you've shown is pretty good and get the badge number up to full, however, I want it to only increase the badge number for the values up to the ID which was saved.So when the view loads it adds the first ID to the user defaults. Then when it's loaded again, it shows the number of objects (as the badge number) until the ID has been reached again. And then when it's loaded again, it'll do the same, showing the badge number as the number of objects until the ID which was previously objectAtIndex:0 and may now be objectAtIndex:4, therefore I'd like the badge to show '4' in it.
Domness
A: 

I ended up doing the follow which seems to be working:

 NSDate *dateRefreshed = [[NSUserDefaults standardUserDefaults] objectForKey:@"dateRefreshed"];

 [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"dateRefreshed"];
 [[NSUserDefaults standardUserDefaults] synchronize];

 [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

 for (NSDictionary *tweetValues in tweets) { // Iterate over all the tweetValues in tweets (assume tweets exist?)

  NSDate *dates = [tweetValues objectForKey:@"created_at"];

  NSTimeInterval sincePostDate = [[NSDate date] timeIntervalSinceDate: dates]; 
  NSTimeInterval sinceNow = [[NSDate date] timeIntervalSinceDate: dateRefreshed];
  int timeDiff = sincePostDate;
  int timeSinceNow = sinceNow;

  if (timeDiff <= timeSinceNow){

    [UIApplication sharedApplication].applicationIconBadgeNumber++; // Increase the badge number by 1
  }

 }

If anyone has any better ideas, please feel free to post the code :)

Domness