iOS — Always ask the users if they want to leave your app

Michael Mavris
Rock and Null
Published in
3 min readJun 16, 2016

If you tap on a telephone number(on Apple’s native apps), iOS will ask you to confirm that you really want to call that number by pressing “Call” or cancel the call by pressing “Cancel”.

Why the users needs to confirm the action?

There are two reasons:

  1. In order to validate the number they are about to call
  2. They have to be aware that the specific action will open another app and put the current app in background.

iOS cares only for calls

For some reason it seems that iOS cares only for calls(with URL Scheme tel:). For every other actions like addresses open with Maps and email address open on Mail, iOS doesn’t ask the users to confirm. So they don’t care at all about the reason 2.

Users must be asked to confirm action before opening any other app.

My opinion is that for every action using the URL Schemes we need to ask the users to confirm. The reason is that some users will click on a button just for curiosity or by mistake, so they probably will open an unwanted app. In my applications I always prompt the users to confirm any actions. For example when the user press on an email, on URL, Facebook page or a telephone number.

The user pressed a button which open a Facebook page
The user pressed a button with a telephone number as title
The user pressed a button with an email as title
The user pressed a button with a URL as title

How to implement it?

That‘s easy. Just create a UIAlertController with title but no message and two buttons, one for cancel and one for confirmation.

The title is a message that helps the users to understand about the action.

As you can see in the screenshots it’s exactly the same as Apple’s implementation, so it will feel familiar to the iOS users.

UIAlertController on each UIViewController?

That would be OK if you will use it only in one view (eg. About View). But if you have a lot of views that using URL Schemes in your application it would be wrong to copy/paste the same code and you will also face consistency issues.

The Solution

In my case the solution was to create a subclass of UIView that behaves like a UIAlertView (which can be called from any controller). That UIView is responsible to check if the app can open the specific URL, show the message of confirmation and then open that app.

MMActionConfirmationView

Configure and use

Just add these three files in your Xcode project from: File-Add Files to YourProjectName-> Select MMActionConfirmation.m, MMActionConfirmation.h and Enum.h -> Click on “Copy items if needed” -> OK

Import the MMActionConfirmation.h and Enum.h in your controller

Set your class as delegate of MMActionConfirmationViewDelegate (Optional)

@interface ViewController : UIViewController<MMActionConfirmationViewDelegate>

Init and show the confirmation alert:

- (IBAction)callAction:(id)sender {

MMActionConfirmationView *mmAC=[[MMActionConfirmationView alloc]initWithConfirmationMode:telephoneConfirmation Value:@"0018008002775" andDelegate:self];

[mmAC show];
}

Implement the MMActionConfirmationCantOpenURLDelegateForMode in your class in order to control the response in case the controller failed to open a url e.g Make a call on iPad

-(void)MMActionConfirmationCantOpenURLDelegateForMode:(NSInteger)mode{
//Handle the error
switch (mode) {
case facebookConfirmation:
NSLog(@"Can't open facebook app");
break;
case telephoneConfirmation:
NSLog(@"Can't make a phone call");
break;
case emailConfirmation:
NSLog(@"Can't open mail app");
break;
case websiteConfirmation:
NSLog(@"Can't open the website");
break;
default:
break;
}
}

You can find here the full project. Feel free to fork and send me a pull request.

PS. The Enum.h is for using enum variables like telephoneConfirmation instead of numbers. You can ignore Enum.h and use NSInteger for modes.

--

--

Michael Mavris
Rock and Null

A curious Mobile Software Engineer. Loves ski, football and rock music!