2014-02-12 33 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 
}