在iOS上自动启动Google身份验证器应用

时间:2015-08-17 05:44:47

标签: ios google-authenticator

是否支持在iOS上启动Google身份验证器?

我想让客户更轻松地打开应用并复制出基于时间的代码,然后再将其粘贴回我的应用中。

我凭经验发现这个(Swift)代码将启动应用程序:

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

...但我想知道是否有更好的,受支持的方式。

具体来说,是否支持不带参数的otpauth://协议来启动应用程序?

2 个答案:

答案 0 :(得分:0)

查看应用程序的Git仓库,看起来他们已经为机器人otpauthtotp注册了自定义URL方案

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

在这里

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

以下是有关如何构建网址的文档:

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

正确构建并在同一设备上获取您的应用和Google身份验证器应用后,您只需要进行测试。

答案 1 :(得分:0)

目标 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]];

}

在 Info.plist 文件中

enter image description here