tags:

views:

811

answers:

4

I'm having a problem with applicationShouldTerminate.

What ever I do it seams that has no effect. Any help would be appreciated.

I'm well versed in programing but this just gives me headache. Im going over some basic tutorials for xcode , as I'm new to mac in general, and am currently looking at a simple flashlight app.

It exists but I would like to add a alert box here with option not to quit.

(void)applicationWillTerminate:(UIApplication *)application
{
    [application setIdleTimerDisabled:NO];
}

this has no effect, alert is closed even before its created.

(void)applicationWillTerminate:(UIApplication *)application
{
    [application setIdleTimerDisabled:NO];
    UIAlertView *alertTest = [[UIAlertView alloc]
                          initWithTitle:@"This is a Test"
                          message:@"This is the message contained
                          with a UIAlertView"
                          delegate:self
                          cancelButtonTitle:@"Button #1"
                          otherButtonTitles:nil];

    [alertTest addButtonWithTitle:@"Button #2"];
    [alertTest show];
    [alertTest autorelease];

    NSLog(@"Termination");
}

I did some reading online and found that it should be possible to do this with

(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender

But no mater where I put that declaration I get error: syntax error before NSApplicationTerminateReply.

There is no syntax error except that xcode seems not to recognize NSApplicationTerminateReply as valid input.

Any sample code would be greatly appreciated.

A: 

I think I found the answer to what I wanted to do but will need to check it when I get back home. Some directions were found here

http://blog.minus-zero.org/

" The iPhone 2.0 software was recently released, and with it came the ability for users to download native apps (i.e., not web sites) directly to their phones from within the iPhone UI or via iTunes. Developers (anyone who pays Apple 59GBP for the privilege) can then write their own apps and have them available for purchase in the App Store.

One limitation of the Apple-sanctioned SDK is that only one application is allowed to be running at a time. This presents a problem for apps such as IM clients, music players and other programs whose functionality relies on being able to run in the background. Another example (courtesy of James) would be an app that takes advantage of the iPhone 3G's GPS chip to create a log of all the places you visit.

However, there is a neat trick that I discovered: your app will only get terminated if you switch away from it, and hitting the iPhone's power button while your app is in the foreground doesn't count as switching away. The upshot of this is you can create apps which continue to run while the iPhone is in your pocket - perfect for the GPS example.

Achieving this is as simple as implementing two methods in your UIApplication delegate - applicationWillResignActive: and applicationDidBecomeActive:. Here's a simple example to demonstrate the effect.

In your UIApplication delegate header file, add a new ivar: BOOL activeApp. Then, in your implementation, add the following three methods:

  • (void)applicationWillResignActive:(UIApplication *)application { NSLog(@"resigning active status..."); activeApp = NO; [self performSelector:@selector(sayHello) withObject:nil afterDelay:1.0]; }

  • (void)applicationDidBecomeActive:(UIApplication *)application { NSLog(@"becoming the active app..."); activeApp = YES; }

  • (void)sayHello { NSLog(@"Hello!"); if (!activeApp) [self performSelector:@selector(sayHello) withObject:nil afterDelay:1.0]; } "

NccWarp9
It sounds like that only applies when the phone goes into standby, and not when they actually switch apps. I'm curious if it will work though.
Eric Petroelje
Eric's right: Those methods are called only when the phone enters standby or when a dialog is shown on top of it (like an incoming SMS alert or the iPod-control popup).
Marco
I should explain more what Im trying to do. I have found a interesting app called "backgrounder" and was wondering what can be done with it so Im trying it out but it seams that when my app goes to background it terminates. I want to check if app is really terminated.
NccWarp9
+3  A: 

I know this is a non-answer, but hopefully I can be helpful:

Displaying a "Really quit?"-type alert like this, even if you can pull it off technically (and I'm not sure you can), is a bad idea and is likely to either cause rejection from the App Store or, at best, an inconsistent user experience because no other apps do this.

The convention with iPhone apps is to save state if necessary, then yield control (for termination) as quickly as possible when the user hits the home button or switches apps.

To ensure a consistent experience, Apple probably has an aggressive timer in place to restrict what you can do in applicationWillTerminate. And even if they don't have a technical measure in place, they probably have an App Store approval policy to ensure that applications quit immediately when they're asked to.

Marco
what about applicationShouldTerminate ?Im currently experimenting with iPhone as a novice to see what can be achieved. Also, im using jailbreaked iPhone with no intention of using appstore, for now. Im having truble implementing it into my applications.
NccWarp9
+2  A: 
  1. applicationShouldTerminate and NSApplication do not exist on the iPhone. You have to use UIApplication.

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

Chris Lundie
Thank you, this explains alot!
NccWarp9
A: 

With regards to the applicationShouldTerminate error, in case anyone's curious, NSApplicationTerminateReply and NSApplication seem to be deprecated...even though the OP's method is exactly how it appears in the docs!

Defining your method as the below should build with no errors:

-(BOOL)applicationShouldTerminate :(UIApplication *)application
OOP_Master