How do you register for UIApplicationWillEnterForegroundNotification in MonoTouch - iphone

I have a problem that the ViewWillAppear method for a UIView does not fire when the application returns from the background. This is a problem, as my main application screen is showing values that are retrieved from the user settings, and if the user has changed these while the application was in the background I need to have the screen refreshed. I have read that one should register for the UIApplicationWillEnterForegroundNotification using NSNotificationCenter.
How do you do this in MonoTouch? Or does anyone know an alternate way of ensuring that the screen is always kept up to date, even when returning from the background?

You could try something along the lines of:
//Register for the notification somewhere in the app
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification, EnteredForeground);
//snip
void EnteredForeground (NSNotification notification)
{
// do your stuff here
}
Bear in mind you would need to do this for every view controller you'd like to update when enterting from the background!

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..

Where to register for applicationWillResignActive using UIStoryboard

Looking at the example in Beginning iOS 5 for persisting your application state, in the first viewController that is shown for the app, they register for applicationWillResignActive in viewDidLoad:. So that makes sense to me in that you register for that notification when your first view is shown.
I'm confused on whether you always do this, or where you typically register for this notification. Q1) Like do they register for this notification in this viewController so they can recreate this view? Q2) If so, do I do this for each viewController?
Q3) I'm using UIStoryboard and my first viewController is a UITabBarController. So do I register for the notification in the first tab's viewController?
I also have a singleton DataManager object that holds the data for the app if that helps anyone guide me in the right direction of where I should save my data. Thanks!
These methods are in the AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
You can perform your actions here. You can however register to "listen" to that event in another view controller (like your example) but that is just to make it easier to send that event notification into that viewcontroller.
1) no, only to ensure that whenver this VC is loaded it will be able to listen to this event.
2) no, only for the ones you want to pass the even easily like this. However using the appdelegate.m and each's vc view did appear is better.
3) depends on the kind of data you want to save, but typically you create your own file and just save it to disk like in any other OS. ios gives you access to the "documents" folder of your app and each folder is used for something specific, read the documentation.

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

Application comes to foreground from background

When application comes to foreground from background then appixationDidBecomeActive gets called. Now, I want to refresh the page from which the home button was tapped. In which method I can write the refresh code. viewWillAppear is not getting called.
In this case viewWillAppear never gets called, you can fire notification(s) whenever -(void)applicationWillEnterForeground:(UIApplication *)application gets called and use it on the view you want.
You can use custom notification like : NSNotificationCenter.
And on call of this you can refresh your view.

Deferentiating the push notification handler when application is foreground and background

It is said that (correct me if I'm wrong) if the application is in the foreground we have to handle push notifications in the "didReceiveRemoteNotification" and if the application is in the background using "didFinishLaunchingWithOptions" when user taps the "view" button of the app. As I dont have a phone to test I want to know whether I am handling this properly.
1) What will be invoked when I taps on the "View" button in the push notification?
2) Let say I am running the application in the foreground and push notification receives at the same time. Will I be given the push notification alert? If so what will happen if the user click on the View button?
3) In this thread How to handle push notifications if the application is already running? it says:
"alert" key will not be there directly under the userInfo dictionary, you need to get another dictionary with name "aps" and then get the "alert" or "body" from "aps" dictionary"
Is this true?
4) I need to push to a certain view when the user clicks on the View button. Hence do I need to handle that code in both methods?
Thank you
There's a nice rundown of the methods invoked by a push notification in this Apple vid: http://developer.apple.com/videos/iphone/#video-advanced-pushnotification - make sure you visit download the full version in iTunes.
This direct link might work: http://developer.apple.com/itunes/?destination=adc.apple.com.3391495696.03391495702.3416205190?i=1378617410
Either way, the general idea is that if your app isn't in the foreground, tapping your view button will trigger didFinishLaunchingWithOptions, and if it is the foreground app, you'll get the didReceiveRemoteNotification.
I don't think you'll get the alert. The method didReceiveRemoteNotification will be called, and it'll be up to you to show a UIAlert if you want.
Yes - that's true.
Yes, but I think you can simplify this by creating a third method specifically designed to handle your view. You can call this from both didFinishLaunching (only if it launched via a notification), and didReceiveRemoteNotification. This way, if your app needs to be launched, you can have time to do any other setup you might need to do for the app to work right out of the get-go (load saved data, init tabbar controllers or anything else like that).
Best of luck