iostabbartargetsalertview

How to show alert view when tab bar item is pressed and how to ignore it when used in a other target?


I have a UITabBar project with 5 tabs.
I am making 2 targets versions out of it: Free and a Paid version.

In the free version, when a user tries to navigate to tab item index 3 or 4, a UIAlertView should appear with a basic message like:

Do you want to upgrade?
YES / Cancel

When pressing Cancel button, the view should go to first view controller.
How should I do that?

Also, my next question, (I am aware that I should make another question here in Stack though) is how to prevent the UIAlertView from showing up in the paid version?

I have come as far as to use a button for UIAlertView for tab item 3 & 4 , but I don't want that.

The 2 targets are functioning well and I use the following code:

- (IBAction)openAlert:(id)sender
{
#ifdef FREE

    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Attention" 
                                                       message:@"Choose option" 
                                                      delegate:self
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:@"Download Full version", nil];
    [alertView show];

#endif
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex ==1) {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString  stringWithFormat:@"http://***.com"]]];
    }
}

Any help would be appreciated.


Solution

  • On the cancel, to move to another UIViewController, you simply change the self.tabBarController object's setSelectedIndex

    example:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        switch (buttonIndex) {
            case 0:
                //Cancel button was clicked
                [self.tabBarController setSelectedIndex:0];
            break;
            case 1:
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString  stringWithFormat:@"http://***.com"]]];
            break;
        }
    }
    

    As for the Free vs Paid, it's opinion-based.
    One way which is basic is by using NSUserDefaults to remember whether the app is free version or paid version and handle your logic accordingly.