【问题标题】:How to log out from google account in ios?如何从ios中的谷歌帐户注销?
【发布时间】:2016-11-29 15:23:51
【问题描述】:

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

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

代码:

override func viewDidLoad() 
{
    super.viewDidLoad()
    GIDSignIn.sharedInstance().uiDelegate = self
    let button = GIDSignInButton(frame: CGRectMake(0, 0, 100, 100))
    button.center = view.center
    view.addSubview(button)
 }

@IBAction func signOutButton(sender: AnyObject) {
     GIDSignIn.sharedInstance().signOut()    
}

【问题讨论】:

  • 你需要清除缓存
  • @Anbu.Karthik - 我通过设置手动完成,然后 Safari 它并不总是有效。如何在 iOS 上使用 Swift 清除 Safari View Controller 的缓存?
  • 看到这可能对你有帮助stackoverflow.com/questions/15064854/…

标签: ios swift ios9 google-plus logout


【解决方案1】:

如果 OP 还在寻找(怀疑它已经 3 年了)并且如果有人仍在研究这个,我想我想通了。

我使用的是 Objective C,但方法还是一样。

我有一个使用 Google 登录进行身份验证的应用。 OP 所描述的这项工作。对于我们的退出,我有以下内容: (IBAction)didTapSignOut:(id)sender { GIDSignIn *gidObject = [GIDSignIn sharedInstance]; [gidObject 签出]; [gidObject 断开连接];

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

}

确保 signOut 在断开连接之前(我最初将这两个颠倒并且它没有将用户注销)。 对于我们的工作流程,我有它,以便出现注销 url。这是有效的,因为如果用户想再次登录,那么他们需要进行身份验证。如果用户已经登录,它会绕过身份验证并直接进入应用程序。

【讨论】:

    【解决方案2】:

    1.下面导入

    import GoogleAPIClient
    import GTMOAuth2
    

    2.在变量下面声明

    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)
      }
      

    这是完整的实现

    【讨论】:

    • 我尝试使用您的代码给我错误“使用未解析的标识符 'GTMOAuth2ViewControllerTouch'。”我使用的是 xcode 7.3 Swift 2.2 外观。
    • @vishalaanuj:现在试试
    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多