0

In my program, I have a code as below. How to exit the program after DETECTING user click OK on the UIAlertView?

Thanks

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"YOur Message" message:@"Your description"
                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
Charles Yeung
  • 38,347
  • 30
  • 90
  • 130
  • 3
    If I'm correct, Apple doesn't allow that kind of behavior. No app can terminate by other means than the home button; your app might be rejected from the App Store, if you intend to submit it. – EmilioPelaez Mar 01 '12 at 10:38

4 Answers4

3

For getting the cancel (your "OK") button Implement this method:

 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
 {
      if(buttonIndex == 0)
          exit(0);
    }
fbernardo
  • 10,016
  • 3
  • 33
  • 46
  • 5
    It will also work nicely at getting your app rejected. Apple tell you explicitly not to do this. – Nick Bull Mar 01 '12 at 10:36
  • 2
    @CharlesYeung: from your point of view - it works. From users point of view - it crashes. Unless you need this for private app that is not inteded for app store of course. – Rok Jarc Mar 01 '12 at 10:50
1

Check the QA here. see this tutorial

if You want then use exit(0);

Deepesh
  • 8,065
  • 3
  • 28
  • 45
  • @Charles Yeung: don't forget to read this part: "Such usage provides a negative experience and is strongly discouraged." :) – Rok Jarc Mar 01 '12 at 10:48
0

set AlertView delegate to self. and do your task in following delegate-

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Naina Soni
  • 720
  • 5
  • 21
0

For capturing the ok pressed, use this:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

   exit(0);
}
Antonio MG
  • 20,382
  • 3
  • 43
  • 62