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

3k views Asked by At

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.

3

There are 3 answers

3
staticVoidMan On BEST ANSWER

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.

1
jailani On

Set your appdelegate as the delegate of your tabbar controller and do this work in appdelegate or whereever

  1. When the user press the cancel button then call

[yourTabbarController setSelectedIndex:0]

  1. Write your code in the following delegate method to avoid alert in particular version(Paid/Free)
  • (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
1
Akshat Singhal On

For the first question, you need to make use of the value of the index of button clicked in the alert view inside (void)alertView: (UIAlertView )alertView clickedButtonAtIndex:(NSInteger)buttonIndex and check if the index of the button pressed is same as the index of the cancel button. Then you can programatically press the tab for the required view and go to that.

Before you load the alert pop up, check for the status of the app, paid or free. You can check the same in two ways, one is by storing the app purchase status on the device in NSUserDefaults, and other by means of a server authentication, though the server authentication would be a hinderance in the use of the app as it would take some time to get the response from the server and in case of no network connectivity, the user would not be able to use the paid features of the app.

In case the app is having the required paid status, simply let the view in the paid tab load else just display a black screen.

You can implement this check in the view controllers of the paid tabs. In viewWillAppear, implement this check for getting the payment status and then if the app is not a paid one, show a black view and present the alert message. Else if the app is paid, business goes as usual.