2014-02-12 32 views
3

我已经设置ClientID的,范围,然后在MyViewController按一下按钮,我打电话从LoginCLass其工作方法登录,但之后[登入验证],委托执行finishedWithAuth是没有得到所谓后不叫。我已经设置的.h文件finishedWithAuth身份验证方法

- (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类型,它们被设置为相同ID捆绑来自Google API访问权限。

这些方法在LoginClass其在MyViewController类使用。

当我将finishedWithAuth委托设置为MyViewController时,通过将其设置为GPPSignInDelegate,代码运行良好。但是,我想从LoginClass中使用它。

任何想法,我要去哪里错了?

+0

找到任何解决这个问题? –

回答

2

这个问题似乎是在GPPSignIn的一个实例是不会离开的应用程序加载Safari和回来到您的应用程序之间持续存在。

目前在您的登录方法您有:

GPPSignIn *signIn = [GPPSignIn sharedInstance]; 

,所以这是一个本地实例变量。试试这个移动到类级:

@implementation LoginClass { 
    GPPSignIn *signIn; 
} 

然后用它在你的登录方法

3

由于您的LoginClass是雨燕类,修复的方法是让它的NSObject的子类。

我有同样的问题:当我试图委托设置为一类,这不是NSObject的一个子类,转让失败,委托保持为零。当我将NSObject添加为超类时,分配按预期工作。

0

在我运行iOS 8的应用程序中,这种情况也发生在我身上。对我来说,这是什么对我来说是一旦启动应用程序就在AppDelegate中设置clientId,而不是在我的UIViewController类的viewDidLoad方法中Google+登录为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]; 

... 
} 
0

下面是这个简单的解决方案。

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 
}