Skip to content

Instantly share code, notes, and snippets.

@charles0126dev
Forked from chancyWu/ iOS exit app friendly
Created November 24, 2016 12:18
Show Gist options
  • Select an option

  • Save charles0126dev/d88dfe4799a4e2b971d6bf1fab28fc4c to your computer and use it in GitHub Desktop.

Select an option

Save charles0126dev/d88dfe4799a4e2b971d6bf1fab28fc4c to your computer and use it in GitHub Desktop.
iOS exit app friendly code snippet
- (IBAction)logOutButton:(id)sender
{
//show confirmation message to user
CustomAlert* alert = [[CustomAlert alloc] initWithTitle:@"Confirmation" message:@"Do you want to exit?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.style = AlertStyleWhite;
[alert setFontName:@"Helvetica" fontColor:[UIColor blackColor] fontShadowColor:[UIColor clearColor]];
[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
NSLog(@"exit(0)");
exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment