How to register for an NSNotification from a UILocalNotification? - iphone

I have a tabbar application and let's say that I want to switch to the second tab and popup an alert at 12:00, even if my application is not running.
I got all the code for UILocalNotification working correctly, but then I thought that the best way to do that would be by posting a notification from the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
// Handle launching from a notification when the app is NOT running
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
[tabBarController setSelectedIndex:1];
[[NSNotificationCenter defaultCenter] postNotificationName:#"AlertNotification" object:self];
}
return YES;
}
Then, in my SecondViewController.m, I have:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popUpAlert:) name:#"AlertNotification" object:nil];
}
But this does not work. I suspect that the notification is sent while the viewDidLoad of the SecondViewController has not been called yet, right? Is it possible to work this out? And do you agree on my approach of using NSNotificationCenter in this case?
Thanks in advance.

I quickly created a test project and got it working by putting the notification registration in awakeFromNib (assuming SecondViewController is created in a xib file)
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popUpAlert:) name:#"AlertNotification" object:nil];
}

I guess, you are right. It doesn't work because you are posting the notification before adding the view controller as observer for it.
Another approach would be to add a bool property to the app delegate to indicate whether the app has been started from the local notification.
The app delegate can be requested from anywhere in the app with [[UIApplication sharedApplication] delegate].

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];

Related

iOS How to tell the App Delegate to present a new view from the current view

I have an iPhone application that receives notifications when the user is not currently using the application. When the user clicks on the notification, they should be brought to a new view when the user opens the application as a result of opening the notification.
The delegate method within the App Delegate I will use to pass my information from the notification is:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
//Pass my data from JSON Payload in here
//Tell current view to go to new view
}
I believe that a reference to a last view controller the user was interacting with before exiting the application would be a method to go about it and storing the Object in NSUserDefaults.
Any suggestions on how I could implement this would be appreciated.
try it with the NSNotificationCenter :) register an observer where the push should be done and fire a notification from your appdelegate. take a look to this tutorial for registration and firering ;)
// AppDelegate
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
//Pass my data from JSON Payload in here
NSObject *obj = myInformationForTheReciever;
[[NSNotificationCenter defaultCenter] postNotificationName:#"goToView" object:obj];
}
-
// MyViewController
-(void)viewDidLoad
{
[super viewDidLoad];
// do your stuff
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(goToView:) name:#"goToView" object:nil];
}
- (void)goToView:(NSNotification*)notification
{
NSObject* myInformationForTheReciever = [notification object];
// do stuff with that
}

Dismiss popover when app goes in background

How to dismiss popover when application enters in background?
You can do this using the delegate method in appdelegate.m file
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//put your dissmiss popover code here
}
it is better to register your controller for UIApplicationDidEnterBackgroundNotification or UIApplicationWillResignActiveNotification and dismiss it whenever your app goes to background, this will make your life quite easier i feel.
registering for notification in your viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMethod)
name:UIApplicationDidEnterBackgroundNotification object:nil];
implement the method and
-(void)myMethod{
// dismiss popview here
}
finally un-register from the notification in your view controller
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Send an NSNotification in your app delegate's willResignActive method, and listen for it in your view controller that contains the popup, and have it dismiss said popover when the notification is received.
try this
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//[popover dissmissPopoverAnimated:YES];
}

I expect the initial view controller of my storyboard to be the current controller

If the application was previously in the background, when applicationDidBecomeActive is called, I expect the initial view controller of my storyboard to be the current controller.
I used:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self.window makeKeyAndVisible];
}
When I restart app ,loginAciton inside rootViewController still be called ,but could not present the next controller . No errors like nothing happened.
- (IBAction)loginAciton:(id)sender
{
id controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Navigation"];
[self presentModalViewController:controller animated:YES];
}
Why?
PS. My rootViewController is not a UINavigationController.
Thanks for any replies.
A much better way is to add UIApplicationExitsOnSuspend to your Info.plist and set it to YES.
I got it !
if you wanna the initial view controller of your storyboard to be the current controller every time , you can try it :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
exit(EXIT_SUCCESS);
}
You can use the notification too, put this code in applicationDidBecomeActive:
[[NSNotificationCenter defaultCenter] postNotificationName:#"appActivated" object:nil];
and add observer in that current view ...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateView:)
name:#"appActivated"
object:nil];
and don't forget to remove the observer in dealloc: of your current view...
[[NSNotificationCenter defaultCenter] removeObserver:self];
may this will help you..

Present view when local notification is received

With iOS 5 and storyboarding, what is the best way to present a view when the user enters the app after having received a localnotification?
I have read that using the NSNotificationCenter is the way to do it but is that also so with storyboarding and segues?
This is exactly how I implemented it. In the AppDelegate's didFinishLaunchingWithOptions: method, I did the following:
UILocalNotification *notification =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
[self application:application didReceiveLocalNotification:notification];
I did this so that I could keep logic in a single place. In the didreceiveLocalNotification: method, I then used the NSNotificationCenter:
// Let another view handle the display
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:#"SHOW_VERSE"
object:self
userInfo:notification.userInfo];
The view that handles the display is the first UIViewController for the Storyboard. In that class, in the viewDidLoad method:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receivedLocalNotification:)
name:#"SHOW_VERSE"
object:nil];
This works very well for me. Hope it helps.

How to present a modal view controller when the app enters in foreground?

I'm trying to present a Modal View Controller when the app enters in foreground.. These are my files:
AppDelegate.m :
#import "AppDelegate.h"
#import "MainViewController.h"
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self.window makeKeyAndVisible];
MainViewController * vc = [[MainViewController alloc]init];
[vc myMethodHere];
}
MainViewController.h :
//[..]
-(void) myMethodHere;
MainViewController.m :
-(void)myMethodHere{
NSLog(#"myMethodHere Activated.");
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
[self presentModalViewController:tweetViewController animated:YES];
}
NSLog(#"myMethodHere Activated.") works.. so I can't understand why "presentModalViewController" doesn't! What should I edit/add? Maybe a delay? Thanks for your help..
p.s. I know my english sucks.. Forgive me :)
I wouldn't rely on the methods in your app delegate for this (even though it seems like the obvious solution) because it creates unnecessary coupling between your application delegate and the view controller. Instead, you can have MainViewController listen for the UIApplicationDidBecomeActive notification, and present the tweet composer view controller in response to this notification.
First, register for the notification in -viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethodHere) name:UIApplicationDidBecomeActiveNotification object:nil];
}
Now, when this notification is received when your app returns from the background, myMethodHere will be invoked.
Lastly, remember to remove yourself as an observer when the view unloads.
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}