Sign Out feature MailCore 2 iOS IMAP

294 Views Asked by At

I am using MailCore 2 iOS IMAP source to have IMAP mail access. I added mail folder list etc. features in that. I want to provide "Sign Out" feature for any imap mail that logged-in. Could anyone advise me, how to clear the existing login session and do sign out successfully?

2

There are 2 best solutions below

3
AudioBubble On

Use the disconnectOperation on your session.

Sample (Swift):

var op :MCOIMAPOperation = currentSession.disconnectOperation()

This logs out the session. You will then need to take care of your local store (deleting your session object, messages etc.). Here's some documentation on it.

0
Conan On

Sample code that working for me

IMAPSessionHelper.swift

class IMAPSessionHelper: NSObject {

   fileprivate static var privateShared: IMAPSessionHelper?
   var imapSession: MCOIMAPSession!

   class func shared() -> IMAPSessionHelper {
      guard let uwShared = privateShared else {
        privateShared = IMAPSessionHelper()
        return privateShared!
      }
      return uwShared
   }

   class func destroy() {
       privateShared = nil
   }

   private override init() {
     imapSession = MCOIMAPSession()
     imapSession.hostname = _IMAP_HOST_NAME
     imapSession.port = _IMAP_PORT
     imapSession.connectionType = .TLS
   }

   func config(with account: Account) {
     imapSession.username = account.email
     imapSession.password = account.password
   }
}

Logout:

fileprivate func logout() {
    let logout = IMAPSessionHelper.shared().imapSession.disconnectOperation()
    logout?.start({ (error) in
        if let error = error {
            logger(error.localizedDescription)
            return
        }

        IMAPSessionHelper.destroy()
        _ = self.navigationController?.popToRootViewController(animated: true)
    })
}