setApplicationIconBadgeNumber when called multiple times does not update the badge - iphone

I am working on an application where in I have to update the badge shown in the app icon multiple times. However, what I have noted is that, the setApplicationIconBadgeNumber api just works once during the lifetime of the app. I have tries using the UILocalNotification, and it works then but, I dont want to follow that route. Have you guys faced a similar problem. If yes, any pointers?
Regards
Nitin

This is a bug in iOS. It's still present today in 6.0.1, where I just fixed it with a work-around:
// Clear app badge number. Work-around for bug in iOS.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Might the problem be from where you are calling it?
Wrong:
// This is only called once during application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
Correct:
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

Related

How to disable iPhone/iPad auto-lock while app is in foreground mode?

I'm developing an music/video player app and just need to konw how to disable the auto-lock while my app is in foreground.
I know I've to use [[UIApplication sharedApplication] setIdleTimerDisabled:YES]; and [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; at some point, but where is the best place to put them?
Enable the idle timer in
- (void)applicationWillResignActive:(UIApplication *)application
and disable it in
- (void)applicationDidBecomeActive:(UIApplication *)application
The best place to disable it is in didFinishLaunchingWithOptions. The system will automatically take care of making the setting have no effect when the app is in the background.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
application.idleTimerDisabled = YES;
return YES;
}
I posted this alternative because the accepted answer does not prevent auto-lock when an alert appears (email, message, calendar event etc), or the notification center or control center is up.
Swift 3.0:
Inside AppDelegate.swift:
application.idleTimerDisabled = true
Outside AppDelegate.swift:
UIApplication.shared().isIdleTimerDisabled = true
And in Swift 3.0:
UIApplication.shared().isIdleTimerDisabled = true
my 2 cents: for xcode 9:
application.idleTimerDisabled = true
.....AppDelegate.swift:28:15: 'idleTimerDisabled' has been renamed to 'isIdleTimerDisabled'
so:
application.isIdleTimerDisabled = true

remove push notification once it is accepted by user

I am pushing some text to an iPhone app. once the message is accepted and processed in the app I still see the message in the notification area and if I accept it again the message get duplicated in the app.
how I can avoid that? Is there away to remove it from the notification area once the push is accepted.
In other words, once
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
is executed, I want to remove the notification from the notification area.
Setting the badge value to 1 or some value first then set it back to 0 worked. Like the following:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Try this maybe?
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];

How to remove badges on springboard icon when application is relaunched?

I am developing an http message related application. In that application when I get a new message from the server, the message count will show on the springboard icon. After reading, the message count will decrease. Now the problem is that my messages count is 2. In that time I removed the application and installed it again. This time the badges count is showing removed application count like 2.
How do I remove the badge count when the application is installed newly?
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
in the app delegate class in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions function put this line of code
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Hope this helps

disable automatic locking on iphone with iOS 4.3.3

I want to disable the auto lock on my iOS4.3.3 program. I found on the web the same answer several times for this and working for xcode3 but I cant find it to work with mine.. help pls? thks
This should work on iOS 4.3.3. If not then you're doing something wrong:
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
I did it in a ViewController like this.
-(void) viewDidAppear:(BOOL)animated
{
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
-(void) viewDidDisappear:(BOOL)animated
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

badge number count increament

I have implemented the local notification concept, it is working properly but there is a problem in badge number it is not increamenting automatically as notifications occurs. I have got a link where solution for this problem is given but don't know how to use it in app delegate. Following is the link... If someone know how to auto increament the badge number ,please provide me some solution.
https://github.com/csheldrick/UILocalNotification
Thanku very much.
When you create your local notification then before registring it with the system add this line of code.
localNotif.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber+ 1;
and then you register you notification with the system
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
localNotif is object of UILocalNotification
Hope this helps
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[UIApplication sharedApplication].applicationIconBadgeNumber=application.applicationIconBadgeNumber+1;
}
i hope it will help ful to you