Search code examples
macosmailcore2

MCOMessageFlag in Swift


I am trying to check message flags, in Swift but am not getting anywhere. I have been referring to this stackoverflow question for help.

Inside a fetchMessagesOperationWithFolder I am getting a selection of messages, seen, flagged, etc.. when I check them, no flags are reported

let requestKind = MCOIMAPMessagesRequestKind.Headers
let uids = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
let folder = "INBOX"

let fetchOperation = self.imapSession.fetchMessagesOperationWithFolder(folder, requestKind: requestKind, uids: uids)

fetchOperation.start { (error, fetchedMessages, vanishedMessages) -> Void in
    if (error != nil) {
        println("Error: \(error)")
    } else {
        for obj in fetchedMessages {
            if let message = obj as? MCOIMAPMessage {

                let messageSeen = (message.flags & MCOMessageFlag.Seen) != nil;
                let messageFlagged = (message.flags & MCOMessageFlag.Flagged) != nil;

                if messageSeen {
                    println("Seen")
                }

            }
        }
    }
}

If I manually set the flags Seen and Flagged are printed as expected..

message.flags = MCOMessageFlag.Seen | MCOMessageFlag.Flagged

let messageSeen = (message.flags & MCOMessageFlag.Seen) != nil;
let messageFlagged = (message.flags & MCOMessageFlag.Flagged) != nil;

if messageSeen {
    println("Seen")
}
...

I am not sure what I am doing wrong, any pointers?


Solution

  • As Dinh Viêt Hoà suggested in a comment, the fetchOperation request headers needed to include the flags.

    let requestKind = MCOIMAPMessagesRequestKind.Headers | MCOIMAPMessagesRequestKind.Flags
    let uids = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
    let folder = "INBOX"
    
    let fetchOperation = self.imapSession.fetchMessagesOperationWithFolder(folder, requestKind: requestKind, uids: uids)