Can I get message when I show UIAlertView - iphone

I want to get message when system show UIAlertView so I can pause my game.
Anyone know how to figure out that?
The UIAlertView is not controlled by myself.

A system alert is normally displayed in its own UIWindow. Install handlers for the UIWindowDidBecomeVisibleNotification and UIWindowDidBecomeHiddenNotification notifications to track when a UIWindow becomes visible and hidden respectively:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(aWindowBecameVisible:)
name:UIWindowDidBecomeVisibleNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(aWindowBecameHidden:)
name:UIWindowDidBecomeHiddenNotification
object:nil];
In the handlers, grab the UIWindow that changes state from the object property of the notification:
- (void)aWindowBecameVisible:(NSNotification *)notification
{
UIWindow *theWindow = [notification object];
NSLog(#"Window just shown: %#", theWindow);
}
- (void)aWindowBecameHidden:(NSNotification *)notification
{
UIWindow *theWindow = [notification object];
NSLog(#"Window just hidden: %#", theWindow);
}
Finally, check that theWindow contains a subview of type UIAlertView.

Application delegate's applicationWillResignActive: will be called on interrupts. You can handle the pause there or you can even listen to the UIApplicationWillResignActiveNotification in your view controller and pause the game there.
You can look at this part of the iOS Application Guide that details the life cycle of the application and state transitions.

If your UIAlertView is from Third party app (not from your app) then you can implement below delegate methods to pause and resume game.
To Pause game
- (void)applicationWillResignActive:(UIApplication *)application {
}
To Resume game
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
For example if you receive call or SMS you can use above delegate to pause/resume game.

Just make this:
- (void)applicationWillResignActive:(UIApplication *)application {
//pause
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
//resume
}

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];
}

handle home button pressing on iphone

I need to handle home button pressing in my app.
When user presses home button in my DetailedViewController I need to trigger method that will [self.navigationController popViewControllerAnimated:YES].
Help me please.
How could it be done?
Check out - (void)applicationWillResignActive:(UIApplication *)application method in you app delegate. It will catch the event. And then your can handle it as you need. For example, post notification using default notification center and get it in class where you need to do something.
What you want exactly? you want to get back to Home(root) or just want to get back to the previous page?
just try out with this code in which you get back to your starting or home controller
[self.navigationControler popToRootViewControllerAnimated:YES];
As Павел Оганесян has described :
// post notification
- (void)applicationWillResignActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"TestNotification" object:self];
}
Now in DetailedViewController .m file
// add observer
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveTestNotification:) name:#"TestNotification"
object:nil];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
// do the needful
}
Hope it helps you...

How to resume process on applicationDidBecomeActive

So I have around 4 viewcontrollers in this iPAd App that I am testing. Before the Application becomes inactive, the TableViewController is presnet. Once I press the button on the iPhone, it will initiate
-(void)applicationWillResignActive:(UIApplication *)application
And when I start it again, I want the application to resume with the process, with the loaded table and show the 'Screen' that was available before I pressed the Button.
I can understand that
-(void)applicationDidBecomeActive:(UIApplication *)application
is involved in this event. Could you tell me how I can actually bring a particular view controller on the event of resuming the process ??
Thanks.
You can register your own UIViewControllers as observers for `UIApplicationDidBecomeActiveNotification.
In your view controllers:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
Do not forget to remove them as observers in their dealloc methods:
- (void)dealloc {
...
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[super dealloc];
}

How to register for an NSNotification from a UILocalNotification?

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];