iOS app badge persists - iphone

I have an iPhone app which uses local notification. It is working almost perfectly, although I have a little problem: The red balloon (app badge) from notification doesn't dismiss when I launch the app. What can I do to fix it?
I've already deleted the app from iPhone, but when I compile it again with Xcode, I comes back again.

Try
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
after you have processed your notification.

Related

How to turn Auto-Lock to Never in Swift?

Is there any Swift command to actually set the Auto-Lock to Never or a specific time period? I want to create a simple app that only has two buttons: one is to set the Auto-Lock to Never and the other one is to set it back to iOS default (1 min).
So when a user open this app and tap the Never button, s/he can open other apps but the iPhone or iPad will never auto lock while running the other apps. If s/he is done with other apps, s/he can open this app again and tap the Default button to set the Auto-Lock back to 1 min.
I understand this can be done from the Settings but I am just curious how I can do it from the backend using Swift.
I am new to Swift, btw.
Thanks much!
So when a user open this app and tap the Never button, s/he can open other apps but the iPhone or iPad will never auto lock while running the other apps
You can't do that. You are sandboxed. You cannot affect what happens to the user while running some other app.
When app is open, try to disable the idleTimer in viewDidLoad
UIApplication.shared.isIdleTimerDisabled = true
When app is closed to open other apps, try to -enable again idleTimer when yourViewController disappears, so put this in viewDidDisappear
UIApplication.shared.isIdleTimerDisabled = true
You need to make use of this API call to set the idle timer disable and enable.
This is in objective-C. Just convert it to swift. The API is available in UIApplication.h
-(void) onApplicationDidActivate:(NSNotification*) notification
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
-(void) onApplicationWillDeactivate
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

Push notification opens app automatically when unlocking an iPad

I have developed the iPad app which uses apple push notification. Push notification delivering works fine in all the scenario except when the iPad device is locked and notification is delivered. In this scenario it behaves weirdly and opens the app when you slide to unlock the device without touching the alert/banner or notification from notification center.
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
[self AgendaView];
}
-(void)AgendaView
{
Agenda_Main *agendaview = [[Agenda_Main alloc]init];
[self.navigationcontroller pushViewController:agendaview animated:YES];
}
This is a feature in iOS.. When u unlock the device, the most recent notification app will be opened..
It works that way when first receiving the alert. If you let the device lock again after receiving it, when you unlock it the next time you will go to Springboard
That is just how iOS works. You'll notice that when the alert comes in, the lock screen will only show information for that alert (it will also be centred on the vertical axis).
If you press the lock button to turn the screen off, then the home button to turn it back on you will see that the notification has now stacked to the top of the window, along with any other notifications. If you unlock the device now it will take you to the springboard, and not the app.

How to cancel all local notifications when app is deleted from background not from device

I want to cancel all local notifications when app deleted from background, not from Device.
I know this is
[[UIApplication sharedApplication] cancelAllLocalNotifications];
but my Question is where should i pass it to fire event when application terminates from background.
I called inside applicationWillTerminateFromBackground but it is not working.
You could simply check to see if the app is in the background, and if it is, not fire the notifications.

Determine if an app is running in the foreground when a notification is received on iOS

I would like to find a way to see what app is running in foreground or if the home screen is displayed when a local notification from my app show up. For example i want to have different actions if there is in homescreen or in someone else app.
I tried to use processed and pid but the pid is generated hen the app starts and not the last time the app is used.
Any idea?
thanks
As described in the push notification documentation you can read [[UIApplication sharedApplication] applicationState] when you receive the notification to determine whether your app is in foreground, inactive (it's visible but a dialog like the WiFi chooser is in front) or in background.
Just to have a copy-paste code available for others:
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
//App is in foreground. Act on it.
}
Swift 5 version:
import UIKit
let isForeground = UIApplication.shared.applicationState == .active

UILocalNotification fires after reinstalling the app

My app has an alarm function using UILocalNotification, and it works great. However, if the user uninstalls the app, then later REINSTALLS it, he would receive all the "in between" notifications at once.
I have tried to call:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if it's the first time the app is launched, but it doesn't help, because the notification is received even before application:didFinishLaunchingWithOptions: is called.
This was worse in 4.0 when the alarm was repeated even if the user has deleted the app, but at least that bug was fixed by Apple in later release. However now I'm stuck with this. Anyone has an idea?
According to Apple, this is not a bug (I filed a bug report). The system retains the UILocalNotifications for uninstalled apps for 24 hours just in case the user deleted the app by accident, and restores the said UILocalNotifications if the app is re-installed within that time frame.
The solution would be to remove all UILocalNotifications on first startup, like so:
- (BOOL) application: (UIApplication*) application
didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
if (self.isFirstRun)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.firstRun = NO;
}
/* Other code here */
...
}
of course, implement your own firstRun setter and getter to fetch/save into persistent storage, like NSUserDefaults.
This is actually a bug in iPhone. If you removed the application and install it later also, it will have same app id, so when the application is reinstalled all the past local notifications were fired even if you didn't open the app.