It is very simple:
first of all - get uid's messages
MCOIndexSet *uidSet = [MCOIndexSet indexSetWithRange:MCORangeMake(1,UINT64_MAX)];//for all msgs MCOIMAPSession *session = <new or reuse IMAP session, i reuse this> MCOIMAPFetchMessagesOperation *fetchOp = [session fetchMessagesByUIDOperationWithFolder:@"INBOX" requestKind:MCOIMAPMessagesRequestKindFullHeaders uids:uidSet]; [fetchOp start:^(NSError *err, NSArray *messagesList, MCOIndexSet *vanished) { if (!err) { NSLog(@"Receive %i messages",(int)[messagesList count]); NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"header.date" ascending:NO]; //upd UI [[NSNotificationCenter defaultCenter] postNotificationName:kNotif_postCollector_ReceiveMessages object:nil]; //store msgs [[DataSingleton sharedSingleton] updateMessageListWithMessages:[messagesList sortedArrayUsingDescriptors:@[sort]]]; }else{ //oh no } }];
second - get the message structure (not everything, just necessary - the messagesList array)
MCOIndexSet *uidSet = [MCOIndexSet indexSet]; for (Message *message in messagesList)// i use this class to store msgs. mcUid = MailCore Uid [uidSet addIndex:[[message mcUid] integerValue]]; MCOIMAPSession *session = <new or reuse IMAP session, i reuse this> MCOIMAPFetchMessagesOperation *fetchOp = [session fetchMessagesByUIDOperationWithFolder:@"INBOX"//or another folder requestKind:MCOIMAPMessagesRequestKindStructure uids:uidSet]; [fetchOp start:^(NSError *err, NSArray *messagesList, MCOIndexSet *vanished) { if (!err) { NSLog(@"Receive %i messages with structures",(int)[messagesList count]); NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"header.date" ascending:NO]; //store attachment precence [[DataSingleton sharedSingleton] updateAttachmentsPresenceForMessages:[messagesList sortedArrayUsingDescriptors:@[sort]] forAccount:acc]; /* You get array of this objects: MCOIMAPPart : MCOAbstractPart //A part identifier looks like 1.2.1 @property (nonatomic, copy) NSString * partID; //The size of the single part in bytes @property (nonatomic, nonatomic) unsigned int size; //It the encoding of the single part @property (nonatomic, nonatomic) MCOEncoding encoding; */ } }];
and step 3 and the last step:
MCOIMAPSession *session = <new or reuse IMAP session, i reuse this> int uid = [[message mcUid] intValue]; NSString *partID = [attachment mcPartID];//we stored it on step 2 MCOIMAPFetchContentOperation * op = [session fetchMessageAttachmentByUIDOperationWithFolder:@"INBOX" uid:uid partID:partID encoding:(MCOEncoding)[[attachment mcEncoding] integerValue]]; NSLog(@"download att %i part %@",uid,partID); [op start:^(NSError * error, NSData * messageData) { if (error) { }else{ TRACE(@"receive att %i part %@",uid,partID); //save attachment fo local disc [[DataSingleton sharedSingleton] updateDownloadedAttachment:attachment withData:messageData]; } }];
Learn more about GitHub and the official libmailcore.com page .
source share