2015-06-16 6 views
8

Sto utilizzando e quando si utilizza il simulatore funziona correttamente perché non è installata l'app Google e l'utente è in esecuzione, ma quando si utilizza il dispositivo iPhone 6 si apre youtube (con alcuni account registrato al loro interno) per il segno maniglia Dopo, quando tornerà sul codice app non entrare in questa funzione:.Non recuperare l'utente Google quando gestisci l'accesso con un'altra app Google utilizzando GIDSignIn

-(void)signIn:(GIDSignIn *) signIn 
    didSignInForUser:(GIDGoogleUser *) 
    user withError:(NSError *) error 

Chiunque può aiutarmi non posso usare un'altra funzione per il login Devo chiamare lo [[GIDSignIn sharedIstance] signIn] e questa funzione rileva se è installata un'altra app Google e apre automaticamente un'altra app Google o Webview.

+0

Qualche fortuna? Non riesco a farlo funzionare .. – Chicken

+0

Anche io sto affrontando lo stesso problema, se hai la risposta per favore fammi sapere come risolverlo. –

risposta

1
import UIKit 
import GoogleSignIn 
import Google 

class ViewController: UIViewController,GIDSignInUIDelegate, GIDSignInDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let gidSingIn = GIDSignIn() 

    GIDSignIn.sharedInstance().uiDelegate = self 
     gidSingIn.delegate = self 
    GIDSignIn.sharedInstance().delegate = self 

    var configureError:NSError? 
    GGLContext.sharedInstance().configureWithError(&configureError) 

    assert(configureError == nil, "Error configuring Google services: \(configureError)") 


    let button = GIDSignInButton(frame:CGRectMake(0,0,30, 200)) 
     button.center = self.view.center 
     button.backgroundColor = UIColor.blueColor() 
     self.view.addSubview(button) 



    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBAction func signInGoogle(sender: AnyObject) { 

    print("pressed") 

} 

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, 
      withError error: NSError!) { 
    if (error == nil) { 
     // Perform any operations on signed in user here. 
     print(user.userID)    // For client-side use only! 
     print(user.authentication.idToken) // Safe to send to the server 
     print(user.profile.name) 
     print(user.profile.givenName) 
     print(user.profile.familyName) 
     print(user.profile.email) 
     print(user.authentication.accessToken) 
     print(user.profile) 
    } else { 
     print("\(error.localizedDescription)") 
    } 
} 
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!, 
      withError error: NSError!) { 
} 

} // Questo è il login con l'account Gmail non per googleplus. copia e incolla nel tuo controller. e aggiungere dopo func nel vostro AppDelegate Classe

func application(application: UIApplication, 
       openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { 
    var options: [String: AnyObject] = [UIApplicationOpenURLOptionsSourceApplicationKey: sourceApplication!,UIApplicationOpenURLOptionsAnnotationKey: annotation] 
    return GIDSignIn.sharedInstance().handleURL(url, 
               sourceApplication: sourceApplication, 
               annotation: annotation) 
} 
4

Ho risolto il problema impostando correttamente alcune proprietà per l'istanza GIDSignIn. Per esempio:

GIDSignIn*sigNIn=[GIDSignIn sharedInstance]; 
[sigNIn setDelegate:self]; 
[sigNIn setUiDelegate:self]; 
sigNIn.shouldFetchBasicProfile = YES; 
sigNIn.allowsSignInWithBrowser = NO; 
sigNIn.allowsSignInWithWebView = YES; 
sigNIn.scopes = @[@"https://www.googleapis.com/auth/plus.login",@"https://www.googleapis.com/auth/userinfo.email",@"https://www.googleapis.com/auth/userinfo.profile"]; 
sigNIn.clientID [email protected]"xxxxxxxxxxxxxxxxxxxxxxxgai.apps.googleusercontent.com"; 
[sigNIn signIn]; 
2

ho intenzione di assumere che si sta utilizzando GIDSignin con il proprio server che richiede di includere un serverclientID con il GIDSignin. Ciò costringerebbe la mia app a provare a utilizzare youtube o google plus per accedere anziché aprire una webview o persino un browser. Ciò restituirebbe un GIDSigninError = -1 "un errore potenzialmente recuperabile .." e non consentirebbe all'utente di accedere.

Il modo in cui ho risolto questo era bloccando gli URL da google o youtube prima che venissero aperti sovrascrivendo l'applicazione di UIA. funzione canOpenURL. Ho fatto questo sottoclassi UIApplication e l'attuazione di canOpenURL come questo:

@interface MyApp : UIApplication 
- (BOOL)canOpenURL:(NSURL *)url; 
@end 

@implementation MyApp 
- (BOOL)canOpenURL:(NSURL *)url 
{ 
    if ([[url scheme] hasPrefix:@"com-google-gidconsent"] || [[url scheme] hasPrefix:@"com.google.gppconsent"]) { 
     return NO; 
    } 
    return [super canOpenURL:url]; 
} 
@end 

int main(int argc, char * argv[]) { 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, NSStringFromClass([MyApp class]), NSStringFromClass([AppDelegate class])); 
    } 
} 

Si noti che di solito ci sarebbe stato un pari a zero dopo argv ma questo è dove si mette il sottoclasse di UIApplication. Questo è anche il modo in cui è possibile utilizzare la propria sottoclasse di AppDelegate.

L'altra soluzione sarebbe quella di creare una categoria sull'applicazione UIA che sovrascrive canOpenURL e utilizzi lo swizzling per chiamare l'implementazione originale all'interno del canOpenURL personalizzato. Questo è un buon articolo su swizzling: https://blog.newrelic.com/2014/04/16/right-way-to-swizzle/

Devo avvertirti, però, queste due soluzioni sono hack e devi stare molto attento agli effetti collaterali che questo può avere sulla tua applicazione. Non sono nemmeno sicuro che Apple sarebbe d'accordo con questo.

0

Il problema per me era che

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!)

non è più esistente, usare al posto

public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!)

nota la differenza didSignInForUser vs. didSignInFor. Molto probabilmente dopo l'aggiornamento dell'SDK a swift 3.