繁体   English   中英

在 iOS 上自动启动 Google Authenticator 应用

[英]Automatically launch Google Authenticator app on iOS

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

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

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

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

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

具体来说,是否支持没有参数的 otpauth:// 协议来简单地启动应用程序?

查看该应用程序的 Git 存储库,他们似乎已经为 bot 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

这是有关如何准确构建 url 的文档:

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

在您正确地形成它们并将您的应用程序和 Google Authenticator 应用程序安装在同一台设备上之后,您只需要进行测试。

目标 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 文件中

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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