applicationWillTerminate problem - iphone

I want to show the UIAlertView when user Click the Iphone Home button( means Close the application)
I have done these Code
- (void)applicationWillTerminate:(UIApplication *)application
{
NSString *errorString =#"Testing";
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:errorString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
But UIAlertView is not Showing. please help
Thanks in Advance

The user aims to close your app when he/she presses the home button. Apple suggest to let him/her to that. What I want to say: Don't do that. I think it is even not possible.

Look at Problem with applicationShouldTerminate on iPhone:
The alert view is never shown because
the 'show' method does not block, and
therefore, the end of
'applicationWillTerminate' is reached
immediately after you create the
alert view and try to show it. I
believe this is by design. You can't
really begin asynchronous operations
in 'applicationWillTerminate'.

applicationWillTerminate: may not be called in the newer version of iOS when pressing the Home button because the app could be only entering the background mode, not actually terminating.
Even if it is actually called (either the app is really terminated, or you're moving the code to applicationWillEnterForeground:), showing the alert is useless because the alert is associated with the active app, and your app has gone inactive by the time the alert is shown! So what happened really is, the alert is gone when the user press the home button, and when they resume your app, they see a mysterious alert popping up.
Don't ask if the user wants to quit your app. This isn't the norm in iOS. Instead, save all states in applicationDidEnterBackground:, and restore them in applicationWillEnterForeground: and application:didFinishLaunchingWithOptions:, making the user feel as if the app has never been terminated.

You might not want to use applicationWillTerminate but rather applicationWillResignActive. Check older posts like this one for more info.

You can't show a uialertview when application receive SIGKILL(Exit) Command.You can call any file or background functions in applicationWillTerminate - To do that you need to set a key in your plist.
UIApplicationExitsOnSuspend - Boolean - YES.

Related

Removing UIAlertView from keyWindow Error

I am removing UIAlertView from UIWindow by using this code : -
UIAlertView *alert= (UIAlertView*)[[[[UIApplication sharedApplication] keyWindow]subviews]objectAtIndex:0];
[alert removeFromSuperview];
After removing alertview from key Window my Whole screen turns faded like suspended while alertview shown. Please tell me how can i make my all screen normal.Thanks in advance.
Don't remove the alert like that. Even if you get it to work now it could easily break with any future update to iOS.
Instead, when you create and show the alert, keep a reference to it. Then, when you need to, call dismissWithClickedButtonIndex:animated:.

iphone Displaying alert with multiple buttons at application's first launch

i want to display an alert at first launch of application, and that alert will contain multiple buttons, like first button title can be student, second can be businessman, third can be professional, 4th can be engineer, means different categorie, and when user presses any button i want to save its title as a string in data base, like if user pressed student button i want to save student in the db, the save into db process is not a problem, but i only want to know that how can i display this kind of alert? and at only the first launch of application and also get value on user click, plz. guide me in this, Regards Saad.
You can set a variable, say firstRun = YES into user preferences.
When you launch the app, check the value from user preferences, if it is true, prompt the alert and do you job.
After that, change the value to NO so next time the user launch it will not prompt the alert. again.
UIAlertView can do that for you, it allows to send an array of strings for the button titles.
Add the code in the app delegate class in applicationDidFinishLaunching, or in the viewDidLoad of the first view controller.
Also use [NSUserDefaults standardUserDefaults] to save the settings. it works like a dictionary. if the value is there then you saved it before so its not the first run. if the value of that key is nil, then this is the 1st run.
Update:
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:#"Error!" message:#"Unable to complete Request.\nPlease try again later" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Demo Button1",#"Demo Button 2",nil];
[alrt show];
[alrt release];
your delegate needs to implement UIAlertViewDelegate to know which button the user pressed. delegate method is -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
as Hanon says but at first time you didn't get the value of firstRun so simply check firstRun is equal to NO and at first time you found firstRun=NO then show your alert and on click on alert button save title in db and also set firstRun to YES so that next time alert could not displayed.

Close UIAlertView and Replace with Another

I'm running an application that requires LocationServices be enabled. I'm checking if they are by making a call to the service and catching the error. In the error case, I want to pop up an alertview notifying the user to activate location services. I have another AlertView open already when this test happens. I want to close that one and give the user the dialog box I mentioned previously.
Currently, I have
case kCLErrorDenied: // CL access has been denied (eg, user declined location use)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"NOTICE"
message:#"Sorry, this application needs your location. Please select 'Allow' when asked to use your current location. You don't need to be on or near the trail."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"EXIT"];
[alert show];
[alert release];
//exit(0);
break;
This causes the app to just exit. I had an NSLog output in there so I know it gets to this case.
here you are specifying delegate:self, then it searches for alert handlers declared at UIAlertViewDelegate and when it doesn't find it crashes.
So you should define
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
in your class.
Also you can implement other methods of UIAlertViewDelegate which will help you in your required task.
You need to keep track of the previous alert with an instance variable, and call the method to dismiss that previous dialog before showing the new one. You also need a delegate handler for the alert.

What class handles the popup/notification windows on iphone?

I'm looking for the class name of the popup/message windows on the iPhone (it's a blueish window that comes up when you have a missed call, or a message comes in for example.)
The class is called UIAlertView. From the documentation:
Use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).
Use the properties and methods defined in this class to set the title, message, and delegate of an alert view and configure the buttons. You must set a delegate if you add custom buttons. The delegate should conform to the UIAlertViewDelegate protocol. Use the show method to display an alert view once it is configured.
The examples you gave (missed call or incoming text message) are system level alerts that pop up over any application. That functionality is not available through the SDK. lajos's answer does provide the correct way to display an alert, but it is worth remembering you can only do this within your application. You cannot pop up an alert over another app because the SDK currently prohibits an app from running in the background.
Further to this response, UIAlertView is indeed the way to do this and the code you want is:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Message" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[alert show];
Here the alert box will popup with the message "Message" and have a single button titled "OK" which will close the popup when clicked. Check the documentation for other things you can do (more buttons etc).

Bug in AlertView on iPhone?

I write a piece of code to "do something->show alert1->do something->show alert2".
//do something
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Alert 1"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
//do something
UIAlertView *alert2 = [[UIAlertView alloc]
initWithTitle:#"Alert 2"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert2 show];
[alert2 release];
And suddenly a strange thing happened to multiple AlertViews: It shows "Alert 1"->"Alert 2"(Press 'OK')->"Alert 1". Why "Alert 1" shows again? I haven't written any delegate method yet. Maybe a bug?(Thanks to cobbal, alert1 is still there when alert2 appears.)
I find -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex works well. Is the delegate method a common way to show multiple alertViews?
I would guess that alert 1 is shown and then covered by alert 2 since show isn't modal. When alert 2 is closed, alert 1 is still open.
To your second question, alertView:didDismissWithButtonIndex: may work better, but I haven't actually tested that.
The delegate is so that you can be notified when the alert is dismissed, and which button was used to dismiss it. It doesn't impact whether the alert is dismissed at all.
The alert will remain visible until it is dismissed either by you tapping a button (if any - they are not required) or you call either [UIAlertView dismissWithClickedButtonIndex:animated] or the (undocumented) dismiss method of the alert instance.
It looks like (as Cobbal suggested), alert 2 is popping up over alert 1, you dismiss alert 2, and alert 1 remains there (until it itself is dismissed).
Is there a particular reason you want to show a new alert while another is still showing? Perhaps some more context would help us to get to the root of the issue, which I suspect may be a design issue.
[edit] coming back to this and reading again, I wonder if what you are asking about with the delegate method is whether you should be showing alert 2 from there? In which case that's probably what you want - whether directly or indirectly. By indirectly I mean that you may have some state set elsewhere that determines whether alert 2 should be shown (or the circumstances that lead to it). That state (a flag, perhaps) could be set when you show the first alert, and cleared when the alert is dismissed (from the delegate method).
The reason this happens is because UIAlertView doesn't block while it's showing. Any code written after showing an alertview will run straight after the alert is shown.
What you should have is two different methods. One that does something and then shows an alert, and then another that does something and shows another alert.
Kick off the first method to do something and show an alert, and then hook into the alert's delegate method, and when you get the callback from the alertview, run the other method.
This way, the second part of the process won't happen until the user has pressed OK on the alert in first part of the process.