Automatically launch Google Authenticator app on iOS
Asked Answered
B

2

7

Is there a supported way of launching Google Authenticator on iOS?

I want to make it easier for customers to open the app and copy out the time-based code, before pasting it back into my app.

I've empirically discovered that this (Swift) code will launch the app:

UIApplication.sharedApplication().openURL(NSURL(string: "otpauth://")!)

...but I want to know if there is a better, supported way.

Specifically, is the otpauth:// protocol supported without arguments to simply launch the app?

Benzel answered 17/8, 2015 at 5:44 Comment(5)
I'm quite sure that's the way you're supposed to do it.Traweek
Thanks @ILikeTau Do you have any inside information or evidence that this is OK?Benzel
Here. Remember to check if you can open the URL with canOpenURL().Traweek
@ILikeTau I've revised question a bit. I understand that openURL and canOpenURL are the standard mechanism to launch other apps. But I want to know that the otpauth:// protocol without arguments supported as a way to launch the app.Benzel
This might helpTraweek
T
3

Looking at the Git repo for the app it does seem like they have registered the Custom URL Schemes for bot otpauth and totp

https://github.com/google/google-authenticator/blob/bd50d15c348a978c314d2b30e586fbc562096223/mobile/ios/OTPAuth-Info.plist#L42

And here

https://github.com/google/google-authenticator/blob/bd50d15c348a978c314d2b30e586fbc562096223/mobile/ios/Classes/OTPAuthURL.h#L23

And here is the documentation on how exactly to build the url:

https://github.com/google/google-authenticator/wiki/Key-Uri-Format

After you form them correctly and get your app and the Google Authenticator app on the same device you would just need to test.

Tamratamsky answered 25/10, 2017 at 1:47 Comment(0)
P
0

Objective C

if ([[[notification realRequestResults] valueForKey:@"action"] isEqualToString:@"2FA"]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Two Factor Authentication"
                                                                       message:@"Please, enter your Google Authenticator 2FA Token."
                                                                preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Confirm Token"
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
            @try {
                NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [userName text], @"loginName",
                                            [passwordField text], @"password",
                                            @"false", @"rememberMe",
                                            [[alert textFields][0] text], @"tfa",
                                            nil];
                [self callWebserviceForIdentifier:AuthRequestInternalLogin
                                   withParameters:parameters
                                onSuccessSelector:@selector(loginSuccessfulAgain:)
                                onFailureSelector:@selector(loginFailedAgain:)];
            } @catch (NSException *exception) {
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"login"
                                                                               message:[NSString stringWithFormat:@"%@", exception.description]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {}];
                [alert addAction:defaultAction];
            } @finally {
            }
        }];
        [alert addAction:defaultAction];
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.delegate = self;
            textField.placeholder = [NSMutableString stringWithString:@"Enter your 2FA token"];
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.font = [UIFont systemFontOfSize:16.0];
            textField.textAlignment = NSTextAlignmentCenter;
            textField.textColor = UIColor.blackColor;
            UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [addButton setImage:[UIImage imageNamed:@"authenticator.png"] forState:UIControlStateNormal];
            [addButton addTarget:self action:@selector(authenticatorBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            textField.rightViewMode = UITextFieldViewModeAlways;
            textField.rightView = addButton;
        }];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Warning"
                                                                       message:@"Invalid Credentials. Please try again."
                                                                preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        [self stopAnimation];
    }
}

-(IBAction)authenticatorBtnClicked:(id)sender{
NSString *AppStoreURL = @"https://apps.apple.com/in/app/google-authenticator/id388497605";
NSString *customAppURL = @"otpauth://";

BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customAppURL]];

NSString *url = canOpenURL ? customAppURL : AppStoreURL;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];

}

In Info.plist File

enter image description here

Portraitist answered 3/6, 2021 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.