iOS UILocalNotification not updating app icon - iphone

first of all, I'm really thankful for all your help.
I need just one more little help.
I finished my app, and I used UILocalNotifications to fire some reminders. The app icon is always with a "1" saying there is 1 new notification on the app but it is like that for ever, even if there is no notification. How can I fix this?
Other detail, when the user slides the app icon when iPhone fires the notification (outside the app when the iPhone is locked or when the app is in the background), I would like the app to load in the main view. Is it possible?

You can reset the badge number inside you application delegate code, in didFinishLaunchingWithOptions and didReceiveLocalNotification: as appropriate. i.e.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
I don't understand your second question.
If you want to change the view it displays depending upon how its launched then you can do so, once again this would be in didFinishLaunchingWithOptions or didReceiveLocalNotification.

Related

Can we lock the app while app goes to sleep mode in IPhone?

I am working an app where lock option is included.My app starts with passcode screen.If I enter correct code then it navigates to next screen.If I dont use the app for long time it goes to sleep mode.When the user wants to run the app now, the passcode screen should appear and the user has to enter the code again.Is it possible?Is there any tutorial for this?Please dont mind to post the related code if you have done it.Thanks in advance.
Yes ofcourse it is possible. You must open the screen in a method called applicationDidBecomeActive in your Application Delegate. This method is called every time the application is opened from background.
So whenever the user starts the already running app, this method will be called and from this you can first show the Password screen, and after that the respective screen.
You can detect when your app goes to the background using the UIApplicationDidEnterBackgroundNotification. When it does, record the date and time. When the user opens the app back up, you will receive UIApplicationWillEnterForegroundNotification. When you receive that, compare the recorded date and time with the current date and time. If that's too old, display the passcode screen.
check in app delegate class there the methods applicationDidEnterForeground and applicationDidEnterBackground are available do your coding there
I have developed same type of apps, where I have implemented this things, For this I made a one Class like this
#interface CommonUIClass:NSObject
+(void)setCurrentViewController:(id)controller;
+(void)openPassWordProtectedScreen;
#end
And
#implementation CommonUIClass
static id currentViewControllerObj;
+(void)setCurrentViewController:(id)controller{
currentViewControllerObj = controller;
}
+(void)openPassWordProtectedScreen{
PROTECTED_CONTROLLER *view = [[PROTECTED_CONTROLLER alloc]init];
if ([currentViewControllerObj respondsToSelector:#selector(presentModalViewController:animated:)]) {
[currentViewControllerObj presentModalViewController:patternLock animated:NO];
}
}
#end
Just import this class to every ViewController And put this code to
-(void)viewWillApear{
[CommonUIClass setCurrentViewController:self];
[super viewWillApear];
}
And When Application Goes in Background
-(void)applicationWillResignActive:(UIApplication *)application{
[CommonUIClass openPassWordProtectedScreen];
}
Thanks..

how to tell what is the current UIView/UIController after an iPhone app in brought back to foreground?

After an iPhone app is put into background, then the user clicks on it again and it comes back to foreground, THEN
Question - How to tell what is the current UIView/UIController after an iPhone app in brought back to foreground?
I know that the application delegate's applicationDidBecomeActive method can be used to trap the return to foreground. I also know that each controller could subscribe via notification centre to UIApplicationDidBecomeActiveNotification.
But if what I want to do is reload the UI data in the UIView/UIController that is displayed, and not carry out this same operation through all other view/controllers, how can I tell which is the one to do the reloadData on?
If your using a nav controller, you can use this:
self.navigationController.visibleViewController

How can I dismiss UILocalNotifications that appear when my app is in the background?

I'm working on an iPhone app that needs to remind a user to check in at regular intervals using UILocalNotifications. If they don't check in for a few hours, they may be reminded a couple times, but I just want to show the latest notification.
Now, if the app is open, I get a callback to didReceiveLocalNotification:(UILocalNotification *)notification, and I can keep track of whether there's a notification showing.
If the app is not running and the user clicks the -action- button, I get a callback to
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
so I can dismiss any old notifications. However, if they click 'cancel', they have to click 'cancel' on a bunch of layered notifications, because as far as I can tell I don't get a callback (how could I, if the app isn't launched) and it doesn't seem like there's a flag or something when creating the UILocalNotification to have newer reminders from an app automatically dismiss other ones.
If the app is in the background but running, it's worse - first, I don't get any sort of callback there if the user click's cancel, so I have the same problem - the user has to click cancel a bunch of times. Second, if they click the action, I get a call to ApplicationDidBecomeActive, but there's no distinguishing between that and when the user just switches back and forth; I can dismiss and reschedule them here, but it doesn't seem to work perfectly, sometimes a few pop up before they're dismissed.
Any suggestions? If there were a way for the notifications to expire automatically that would be great too. I've looked online a bit and haven't found much help, but it seems like a big oversight, so hopefully there's some way to handle this gracefully.
Thanks.
You won't be able to get any callback when the user "cancel"s as you pointed out.
Is it possible to just remind the user once in your case? Only schedule one notification at a time and renew it on app launch/resume.
I have not tried the following yet, but I believe this can be a work around for your case. Use CLLocationManager's startMonitoringSignificantLocationChanges
Every time the device's location changes significantly, your app will be launched in the background with options passed to locationManager:didUpdateLocations: and you can probably schedule a UILocalNotification from there!

iPhone background app showing Call alert

I am new to iPhone programming and I have a question. If I have an application that needs to initiate a phone call at 11:00 AM, is it possible for the app to show a call alert to the user? so the user at 11 would see the number pop up with a call or cancel option. Thoughts? Also is this also possible if my app is not running in the background and If I have to initiate using push notification.
You can do this by using a local notification that will bring your application to foreground (well, sort of, an alert will be displayed that will allow the user to take the appropriate action).
In order to launch a phone call you are going to use the openURL method on the application object like this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:12345689"]];

Is there a way to make my background iPhone app to go foreground?

Is there a way to make my background iPhone app to go foreground?
Tnx.
A UILocalNotification will bring the app to the foreground if the device is locked, a notification appears, and the user unlocks the device.
A UILocalNotification with an alertAction will display the alert while the device is unlocked, and if the user taps the View button (or whatever you set it to), your app will be brought to the front.
Not for your app, but the user could do it.
You could schedule a UILocalNotification to inform the user that you are done with your task or whatever.
Nope. But you can do something when you feel must do. For example, use remote Apple Push Notification Service (APNS) to notify your user that you want her to bring your app back to the foreground.
Or, as JustSid put it, use local notification to notify your user that you want her attention.