5

是否有在 iOS 上启动 Google Authenticator 的受支持方式?

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

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

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

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

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

4

2 回答 2

1

查看应用程序的 Git 存储库,他们似乎已经为机器人注册了自定义 URL 方案,otpauth并且totp

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 应用程序放在同一台设备上之后,您只需要进行测试。

于 2017-10-25T01:47:12.983 回答
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 文件中

在此处输入图像描述

于 2021-06-03T06:49:57.950 回答