ios - ios 如何退出谷歌账户?

标签 ios swift ios9 google-plus logout

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

enter image description here

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

enter image description here

代码:

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

最佳答案

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)
}
  • 最后使用注销

     func logout(){
    //logout code
    GTMOAuth2ViewControllerTouch.removeAuthFromKeychainForName(kKeychainItemName)
    navigationController?.popViewControllerAnimated(true)
    }
    
  • 这是完整的实现

    关于ios - ios 如何退出谷歌账户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38582083/

    相关文章:

    ios - 避免在 View 中再次获取图像

    ios - 在 Xcode 11.2 中使用 Storyboards 以编程方式设置初始 View Controller

    swift - PHAssetCreationRequest 不起作用

    ios - `marquee` 中的 `objective c` 标签是否有替代品

    IOS 应用无法通过在 FQL 中调用 me() 来获取 Facebook uid

    swift - 单击 alertView 中的按钮时如何导航 ViewController

    ios - 如何获取 SKAction(名称 :) to work reliably?

    objective-c - 如何找出我的 SSL 设置有什么问题?

    iOS强制App使用自定义键盘

    ios - FSCalendar (Swift 4) 如何在特定日期开始一周