IOS 8 Google+ Authentication trySilentAuthentication Fails

I am writing an app in Swift and using the Google+ SDK. I have successfully traversed Swift and Objective C code. My problem is that after successful user authentication, if the user closes and opens the app, the trySilentAuthentication method always fails. I've done some debugging here so things aren't as clean as I plan to do them, but in my login controller I have the following:

var signIn : GPPSignIn = GPPSignIn.sharedInstance()
signIn.shouldFetchGooglePlusUser = true;
signIn.shouldFetchGoogleUserEmail = true;  // Uncomment to get the user email

// You previously set kClientId in the "Initialize the Google+ client" step
signIn.clientID = appDelegate.gpClientId;

// Uncomment one of these two statements for the scope you chose in the previous step
signIn.scopes.append(kGTLAuthScopePlusLogin);  // "https://www.googleapis.com/auth/plus.login" scope
//signIn.scopes = @[ @"profile" ];             // "profile" scope

// Optional: declare signIn.actions, see "app activities"
signIn.delegate = self;

if(signIn.authentication == nil)
{
  var signInButton : GPPSignInButton = GPPSignInButton()
  signInButton.center = self.view.center;
  signInButton.center.y += 60
  self.view.addSubview(signInButton)
}

      

... and elsewhere:

  func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!)
  {
      var now : NSDate = NSDate(timeIntervalSinceNow: 0)
      if(error == nil && auth.expirationDate.compare(now) == NSComparisonResult.OrderedDescending)
      {
          self.dismissViewControllerAnimated(true, completion: { () -> Void in
            NSLog("LoginViewController dismissed")
          })
      }
  }

      

This works well and the view manager rejected itself indicating that the login was successful. Now, for a bit of a hacky bit. In my AppDelegate.swift file I have the following:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
  var signIn : GPPSignIn = GPPSignIn.sharedInstance()

  if(signIn.authentication == nil)
  {
    signIn.shouldFetchGooglePlusUser = true;
    signIn.shouldFetchGoogleUserEmail = true;  // Uncomment to get the user email

    // You previously set kClientId in the "Initialize the Google+ client" step
    signIn.clientID = gpClientId;

    // Uncomment one of these two statements for the scope you chose in the previous step
    signIn.scopes.append(kGTLAuthScopePlusLogin);  // "https://www.googleapis.com/auth/plus.login" scope
    //signIn.scopes = @[ @"profile" ];             // "profile" scope

    // Optional: declare signIn.actions, see "app activities"
    signIn.delegate = self;

    if(signIn.trySilentAuthentication())
    {
      self.userLoggedIn = true;
      NSLog("Silent G+ login successful (initially)")
    }
    else
    {
      NSLog("G+ silent authentication failed.")
    }
  }

  // Override point for customization after application launch.
  return true
}

      

The weird thing is, when I open the app a second time (i.e. after logging in for the first time) and click on the login button, it fires a callback immediately finishedWithAuth

, without even calling the URL handler. It's like it knows that I was registered earlier, but silent authentication still doesn't work. Any ideas?

+3


source to share





All Articles