如何在ios中退出Google帐户?

时间:2016-07-26 06:01:55

标签: ios swift ios9 google-plus logout

在我的应用程序中,我可以选择使用谷歌登录登录应用程序。登录工作正常。点击退出按钮后,我无法从谷歌退出。当我点击登录按钮时,它没有显示登录页面,如下图所示:

enter image description here

而是将其重定向到身份验证对话框页面,如下图所示:

enter image description here

代码:

ssl_wrap

2 个答案:

答案 0 :(得分:0)

1.进口以下

import GoogleAPIClient
import GTMOAuth2

2.declare低于变量

let kKeychainItemName = "your app name"
let kClientID = "your app clinet id"
let scopes = [kGTLAuthScopeDrive]
let service = GTLServiceDrive()

3.将此方法替换为现有方法

override func viewDidLoad() {
    super.viewDidLoad()

    if let auth = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychainForName(
        kKeychainItemName,
        clientID: kClientID,
        clientSecret: nil) {
        service.authorizer = auth
    }


    self.tblView.tableFooterView=UIView()
    // Do any additional setup after loading the view.
}

override func viewDidAppear(animated: Bool) {
    if let authorizer = service.authorizer,
        canAuth = authorizer.canAuthorize where canAuth {
        fetchFiles()
    } else {
        presentViewController(
            createAuthController(),
            animated: true,
            completion: nil
        )
    }
}
func fetchFiles() {
    let query = GTLQueryDrive.queryForFilesList()
    query.pageSize = 10
    query.fields = "nextPageToken, files(id, name)"
    service.executeQuery(
        query,
        delegate: self,
        didFinishSelector: #selector(GoogleDriveVC.displayResultWithTicket(_:finishedWithObject:error:))
    )
}

// Parse results and display
func displayResultWithTicket(ticket : GTLServiceTicket,
                             finishedWithObject response : GTLDriveFileList,
                                                error : NSError?) {
    if let error = error {
        showAlert("Error", message: error.localizedDescription)
        return
    }
    if let files = response.files where !files.isEmpty {
        for file in files as! [GTLDriveFile] {
            self.arrayOfNames.append(file.name)
            self.arrayOfIdentifier.append(file.identifier)
        }
    }
    self.tblView.reloadData()
}

// Creates the auth controller for authorizing access to Drive API
private func createAuthController() -> GTMOAuth2ViewControllerTouch {
    let scopeString = scopes.joinWithSeparator(" ")
    return GTMOAuth2ViewControllerTouch(
        scope: scopeString,
        clientID: kClientID,
        clientSecret: nil,
        keychainItemName: kKeychainItemName,
        delegate: self,
        finishedSelector: #selector(GoogleDriveVC.viewController(_:finishedWithAuth:error:))
    )
}

// Handle completion of the authorization process, and update the Drive API
// with the new credentials.
func viewController(vc : UIViewController,
                    finishedWithAuth authResult : GTMOAuth2Authentication, error : NSError?) {

    if let error = error {
        service.authorizer = nil
        showAlert("Authentication Error", message: error.localizedDescription)
        return
    }

    service.authorizer = authResult
    dismissViewControllerAnimated(true, completion: nil)
}

// Helper for showing an alert
func showAlert(title : String, message: String) {
    let alert = UIAlertController(
        title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.Alert
    )
    let ok = UIAlertAction(
        title: "OK",
        style: UIAlertActionStyle.Default,
        handler: nil
    )
    alert.addAction(ok)
    presentViewController(alert, animated: true, completion: nil)
}
  1. 最后使用

    注销
     func logout(){
    //logout code
    GTMOAuth2ViewControllerTouch.removeAuthFromKeychainForName(kKeychainItemName)
    navigationController?.popViewControllerAnimated(true)
    }
    
  2. 这是完整的实施

答案 1 :(得分:0)

如果OP仍在寻找(因为已经3年了,所以对此表示怀疑),并且如果有人仍在研究这个问题,我想我已经解决了。

我正在使用Objective C,但是方法仍然相同。

我有一个使用Google登录进行身份验证的应用。如OP所描述的那样。对于我们的登出,我有以下内容: (IBAction)didTapSignOut:(id)发送者 { GIDSignIn * gidObject = [GIDSignIn sharedInstance]; [gidObject注销]; [gidObject断开连接];

NSString *logOutUrl = @"https://www.google.com/accounts/Logout";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: logOutUrl] options:@{} completionHandler:nil];

}

确保在断开连接之前签出(我本来是将这两个相反,并且未将用户注销)。 对于我们的工作流程,我拥有它,以便显示注销URL。之所以可行,是因为如果用户要再次登录,则需要进行身份验证。如果用户已经登录,它将绕过身份验证并直接进入应用程序。