Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView button action?

I have an UIAlertView that shows with this code that asks you to rate the application in the appstore.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate on the Appstore!" 
                                                message:@"" 
                                               delegate:self 
                                      cancelButtonTitle:@"Later" 
                                      otherButtonTitles:@"OK", nil];
[alert show];
[alert release];

But I cannot figure out how to add an action to the OK button that takes you to the app in the AppStore.

like image 561
JohnAnge Kernodle Avatar asked Apr 21 '12 01:04

JohnAnge Kernodle


2 Answers

How about this?

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex]) {
        NSLog(@"Launching the store");
        //replace appname with any specific name you want
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/appname"]];
    } 
}
like image 128
CodaFi Avatar answered Nov 11 '22 12:11

CodaFi


You want something like the following:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"Clicked button index 0");
        // Add the action here
    } else {
        NSLog(@"Clicked button index other than 0");
        // Add another action here
    }
}

NSLog's appear in the console when you press a button and help out whenever you want to debug/test anything.

Then for the action that you want, you'd write something like:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"url_to_app_store"]];
like image 20
Domness Avatar answered Nov 11 '22 12:11

Domness