2016-07-26 102 views
0

在我的应用程序中,我有一个选项,使用谷歌登录登录到应用程序。登录工作正常。一旦我点击注销按钮,我无法从谷歌注销。当以往我点击登录按钮,它没有显示的图像下面给出的登录页面:如何从iOS帐户注销?

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

你需要清除缓存 –

+0

@ Anbu.Karthik - 我已经通过设置手动完成,然后Safari浏览器它不会总是工作。如何在iOS上清除Safari View Controller的缓存? –

+0

看到这可能是帮助ypu http://stackoverflow.com/questions/15064854/delete-files-from-nscachesdirectory-programmatically –

回答

0

1.import below

import GoogleAPIClient 
import GTMOAuth2 

下面可变

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

3.更换2.declare这个方法到现有的一个

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) 
    } 
    
  • 这是完整的实施

    +0

    我试着用你的代码给我错误“使用未解析的标识符'GTMOAuth2ViewControllerTouch'”。即时通讯使用xcode 7.3 Swift 2.2外观。 –

    +0

    @vishalaanuj:现在试试 –