在authenticate方法之后没有调用finishedWithAuth
我已经设置了clientID,范围,然后在MyViewController中单击按钮,我从LoginCLass调用方法登录,但是在[signIn authenticate]之后,委托实现finishedWithAuth没有被调用。我已将.h文件设置为
- (NSError *) login { NSLog(@"In login :%@ %@ %@",kClientId,scopes,actions); if(!kClientId|| !scopes) { NSLog(@"Either client ID Or scope is not specified!! "); NSError *err=[[NSError alloc]initWithDomain:@"Scope not specified" code:0 userInfo:nil]; return err; } else { NSLog(@"Client ID and Scope are set."); GPPSignIn *signIn = [GPPSignIn sharedInstance]; signIn.delegate = self; signIn.shouldFetchGooglePlusUser = YES; [signIn authenticate]; [signIn trySilentAuthentication]; return nil; } } - (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error { NSLog(@"Received error %@ and auth object %@",error, auth); }
而且在AppDelegate中我正在添加代码
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation]; }
我已检查要指定的软件包ID和URL类型,它们与Google API访问中的软件包ID相同。
这些方法在LoginClass中,在MyViewController类中使用。
当我将finishedWithAuth委托设置为MyViewController时,通过将其设置为GPPSignInDelegate,代码运行正常。但是,我想在LoginClass中使用它。
任何想法,我哪里错了?
由于您的LoginClass是一个Swift类,修复方法是使它成为NSObject的子类。
我遇到了同样的问题:当我尝试将委托设置为不是NSObject的子类的类时,分配失败并且委托仍然为零。 当我将NSObject作为超类添加时,赋值按预期工作。
问题似乎是在离开应用程序加载Safari并返回到您的应用程序之间不会持久存在GPPSignIn的实例。
目前在您的登录方法中,您有:
GPPSignIn *signIn = [GPPSignIn sharedInstance];
所以这是一个本地实例变量。 尝试将其移至课程级别:
@implementation LoginClass { GPPSignIn *signIn; }
然后在您的登录方法中使用它
您可以使用
if (![signIn trySilentAuthentication]) [signIn authenticate];
请点击此链接了解更多详情
没有Google+按钮的trySilentAuthentication
在我运行iOS 8的应用程序中也发生了这种情况8.对我来说,一旦应用程序启动,就在AppDelegate中设置clientId,而不是在我的UIViewController类的viewDidLoad方法中,如Google+登录中所示适用于以下url中的iOS示例: https : //developers.google.com/+/mobile/ios/sign-in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { / //Google+ // Set app's client ID for |GPPSignIn| and |GPPShare|. [GPPSignIn sharedInstance].clientID = @"xxxxxx.......apps.googleusercontent.com"; ... return YES; }
因此,在您的UIViewController类中,登录方法应该是:
- (void)viewDidLoad { [super viewDidLoad]; //Google+ for Logging in the user again if the app has been authorized signIn = [GPPSignIn sharedInstance]; signIn.shouldFetchGooglePlusUser = YES; signIn.shouldFetchGoogleUserID = YES; signIn.shouldFetchGoogleUserEmail = YES; signIn.scopes = @[ kGTLAuthScopePlusLogin ]; signIn.delegate = self; [signIn trySilentAuthentication]; ... }
这是一个简单的解决方案。
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { if (url.absoluteString.range(of: "oauth2callback") != nil) { GPPSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: nil) } return false }