2011年11月30日 星期三

Close the app with NSTimer

A. 新增一個專案:
   Xcode > File > New > New Project...
   iOS > Application > Empty Application

   Product Name: CloseApp
   Device Family: iPhone
   checked: Use Automatic Reference Couting

---------------------------------------------------------------------------------------------

B. 修改 AppDelegate.h 如下:
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    //@add
    NSNumber *myNumber;
}

//@add
@property (nonatomic, strong) NSNumber *myNumber;

@property (strong, nonatomic) UIWindow *window;

//@add
- (void)setProgress;
- (void)goProgress:(NSTimer *)theTimer;

@end

---------------------------------------------------------------------------------------------

C. 修改 AppDelegate.m 如下:
#import "AppDelegate.h"
@interface AppDelegate()
- (void)terminateWithSuccess;
@end   

@implementation AppDelegate
//@add
@synthesize myNumber = _myNumber;

//@add
-(NSNumber *)myNumber
{
    if (!_myNumber) {
        _myNumber = [[NSNumber alloc] initWithInt:0];
    }
    return _myNumber;
}

//@add
- (void)terminateWithSuccess
{
}

//@add
- (void)setProgress
{
    [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(goProgress:)
                                   userInfo:nil
                                    repeats:YES];
}

//@add
- (void)goProgress:(NSTimer *)theTimer {
   
    int myInt = [self.myNumber intValue]+1;
    self.myNumber = [NSNumber numberWithInt:myInt];
    NSLog(@"myNumber = %@", self.myNumber);
   
    if (myInt > 10) {
        [theTimer invalidate];
       
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(terminateWithSuccess)]) {

            NSLog(@"call terminateWithSuccess");

            [[UIApplication sharedApplication] performSelector:@selector(terminateWithSuccess)];

        } else {
            NSLog(@"call exit(0)");
            exit(0);
        }
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
   
    //@add
    [self setProgress];
   
    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
    //@add
    NSLog(@"call applicationWillTerminate");
}

---------------------------------------------------------------------------------------------

D. 說明:
   1. terminateWithSuccess 是 Apple 的  non-public API, 提交程式時有可能不會
       通過審核, 不過它會呼叫到 applicationWillTerminate: method.  

   2. 或直接使用 exit(0); 來離開程式, 不過它不會呼叫到 applicationWillTerminate:
        method.
 

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。