Proper way to exit iPhone application?

前端 未结 25 2696
难免孤独
难免孤独 2020-11-22 01:54

I am programming an iPhone app, and I need to force it to exit due to certain user actions. After cleaning up memory the app allocated, what\'s the appropriate method to ca

25条回答
  •  渐次进展
    2020-11-22 02:46

    exit(0) appears to a user as crashes, so show a confirmation message to user. After confirmation suspend(home button press programmatically) and wait 2 seconds while app is going background with animation then exit behind user's view

    -(IBAction)doExit
    {
        //show confirmation message to user
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                     message:@"Do you want to exit?"
                                                    delegate:self
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"OK", nil];
        [alert show];
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex != 0)  // 0 == the cancel button
        {
            //home button press programmatically
            UIApplication *app = [UIApplication sharedApplication];
            [app performSelector:@selector(suspend)];
    
            //wait 2 seconds while app is going background
            [NSThread sleepForTimeInterval:2.0];
    
            //exit app when app is in background
            exit(0);
        }
    }
    

提交回复
热议问题