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