我正在使用 GTMOAuth2 通过我的企业应用程序在 iOS 上登录。场景是只有帐户 @mycompanyname.com 的用户可以登录。
问题是我正在调用文档提供的方法,但我无法检索用户用于登录的电子邮件帐户。
登录时调用如下:
_auth = [self authForGoogle];
// Display the authentication view
GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:_auth
authorizationURL:[NSURL URLWithString:GoogleAuthURL]
keychainItemName:@"GoogleKeychainName"
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[_window setRootViewController: viewController];
[_window makeKeyAndVisible];
在我的 GTMOAuth2 委托方法中,我这样做是为了尝试检索电子邮件地址:
- (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController
finishedWithAuth:(GTMOAuth2Authentication * )auth
error:(NSError * )error
{
if ([[_auth userEmail] rangeOfString:@"@mycompanyname.com"].location == NSNotFound && error != nil ) {
// Loop for no mycompanyname mail domain and also error in login
NSLog(@"Loop 1 - Non mycompanyname domain email OR Google login error.");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please Login with your mycompanyname email."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
} else if ([[_auth userEmail] rangeOfString:@"@mycompanyname.com"].location == NSNotFound) {
// Loop for no error in login but without mycompanyname mail domain
NSLog(@"Loop 2 - Non mycompanyname domain email AND no Google login error.");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Authentication Denied"
message:@"Please Login with your mycompanyname email."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
} else if (error != nil) {
NSLog(@"Loop 3 - mycompanyname domain email AND Google login error.");
if ([[error localizedDescription] rangeOfString:@"error -1000"].location != NSNotFound)
{
NSLog(@"Loop 3.1 - Error message contains 'error -1000'.");
// Loop to catch unwanted error message from GTMOAuth which will show if user enters the app and decides not to sign in but enters the app after again.
} else
{
// Loop for error in authentication of user for google account
NSLog(@"Loop 3.2 - Error message caused by no internet connection.");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Your internet connection seems to be lost."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
} else {
// Loop for mycompanyname mail domain and no authentication error
NSLog(@"Loop 4 - mycompanyname domain email AND no Google login error.");
// initialize app
}
}
问题是我无法获取尝试登录的用户的正确电子邮件地址,以便能够准确检查电子邮件地址并仅在之后初始化应用程序。
我已经对错误进行了检查,并且全部使用@gmail.com 地址登录,这解释了为什么代码很长。
请帮忙!提前致谢!