iOS MAilCore:如何下载邮件附件并在 web 视图中显示?
Posted
技术标签:
【中文标题】iOS MAilCore:如何下载邮件附件并在 web 视图中显示?【英文标题】:iOS MAilCore: How can i download mail attachments and show it in a webview? 【发布时间】:2013-07-27 12:22:10 【问题描述】:我是 ios 开发的新手。我正在使用 MailCore 在我的应用程序中接收电子邮件。我的 xcode 版本是 4.6.3 和 iOS:6.1 和 5.1。谁能帮我解决我的问题。我从我的服务器收到带有附件计数和描述的邮件,但我没有找到下载此文件的方法或我如何获取附件。
请帮帮我。
谢谢!
【问题讨论】:
【参考方案1】:很简单:
首先——获取消息的uid
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
];
第二 - 获取消息的结构(不是全部,只需要 - 数组消息列表)
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's the encoding of the single part
@property (nonatomic, nonatomic) MCOEncoding encoding;
*/
];
第三步和最后一步:
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];
];
更多信息在GitHub和官方页面libmailcore.com
【讨论】:
您好,您可以发布您的 DataSingleton 课程吗?谢谢 @sundsx 抱歉,没有。它受 NDA 保护。你想知道什么? 嗨,winsergey。我想按照您的示例存储附件。例如:updateAttachmentsPresenceForMessages 和 updateDownloadedAttachment 是数组还是 nsdata 对象? @sundsx 回答你的问题:gist.github.com/b5684c470e877af75880gist.github.com/ba53ef49818ea6db79be 你是个大人物!谢谢 WINSergey。【参考方案2】: MCOMessageParser * msg = [MCOMessageParser messageParserWithData:data];
if ([_message.attachments count] > 0)
for (int i=0; i<[_message.attachments count]; ++i)
MCOIMAPPart *part = [_message.attachments objectAtIndex:i];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@",
docDir, [part filename]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (NO == fileExists)
[data writeToFile:filePath atomically:YES];
【讨论】:
以上是关于iOS MAilCore:如何下载邮件附件并在 web 视图中显示?的主要内容,如果未能解决你的问题,请参考以下文章
MailCore2 - 在不下载图像或附件的情况下获取邮件内容?