A simple tutorial to add badges to iphone/ipad icons, and then to increase decrease their values.

The Apple Push Notification Service is a service created by Apple Inc. that was launched together with iOS 3.0 on June 17, 2009. It uses push technology through a constantly-open IP connection to forward notifications from the servers of third party applications to the Apple devices; such notifications may include badges, sounds or custom text alerts. APNS was also added as an API to Mac OS X v10.7 "Lion" for developers to take advantage of.


In this article I'll show you how to add a badge counter to your icon app with or without the use of the push notifications.

1. First of all, open the Xcode and create a new project.

2. Select the "Single View Application" and click "next"

3. Give a name to your new app and click the "next" button.

4. Select the MainStoryboard.storyboard and add the increase and the decrease buttons

5. Select the AppDelegate.h file and add the increaseBadges and the decreaseBadges IBAction's, you should have someting like the following

//

// AppDelegate.h

// iconBadge

//

// Created by Luigi D'Andrea on 20/03/12.

// Copyright (c) 2012 xAppSoftware.com. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

{

int badgeNumber;

}

 

@property (strong, nonatomic) UIWindow *window;

 

-(IBAction)increaseBadges:(id)sender;

-(IBAction)decreaseBadges:(id)sender;

 

@end

 

6. Select the AppDelegate.m file and add the increaseBadges and the decreaseBadges IBAction's, you should have someting like the following

 

/**

………………

……………..

.code

code

code

*/

 

-(IBAction)increaseBadges:(id)sender

{

badgeNumber++;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: badgeNumber];

}

-(IBAction)decreaseBadges:(id)sender

{

badgeNumber–;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: badgeNumber];

 

}

 

@end

 

7. Select the MainStoryboard.storyboard and link the increase button with the increaseBadges action in the first responder and the decrease button with the decreaseBadges action in the firstResponder.

 

 

Last click the run button and try to see if the icon badge is changing.

Gg1