简体   繁体   中英

Automatically launch Google Authenticator app on iOS

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?

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM