在MailCore 2中搜索电子邮件功能(iOS / Swift)

时间:2016-10-13 10:26:03

标签: ios objective-c swift xcode mailcore2

我正在实施MailCore2以在我的应用中获取用户的电子邮件。我已成功获取用户的电子邮件,代码如下: -

- (void)loadAccountWithUsername:(NSString *)username
                       password:(NSString *)password
                       hostname:(NSString *)hostname
                    oauth2Token:(NSString *)oauth2Token
{
    self.imapSession = [[MCOIMAPSession alloc] init];
    self.imapSession.hostname = hostname;
    self.imapSession.port = 993;
    self.imapSession.username = username;
    self.imapSession.password = password;
    if (oauth2Token != nil) {
        self.imapSession.OAuth2Token = oauth2Token;
        self.imapSession.authType = MCOAuthTypeXOAuth2;
    }
    self.imapSession.connectionType = MCOConnectionTypeTLS;
    MasterViewController * __weak weakSelf = self;
    self.imapSession.connectionLogger = ^(void * connectionID, MCOConnectionLogType type, NSData * data) {
        @synchronized(weakSelf) {
            if (type != MCOConnectionLogTypeSentPrivate) {
//                NSLog(@"event logged:%p %i withData: %@", connectionID, type, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
            }
        }
    };

    // Reset the inbox
    self.messages = nil;
    self.totalNumberOfInboxMessages = -1;
    self.isLoading = NO;
    self.messagePreviews = [NSMutableDictionary dictionary];
    [self.tableView reloadData];

    NSLog(@"checking account");
    self.imapCheckOp = [self.imapSession checkAccountOperation];
    [self.imapCheckOp start:^(NSError *error) {
        MasterViewController *strongSelf = weakSelf;
        NSLog(@"finished checking account.");
        if (error == nil) {
            [strongSelf loadLastNMessages:NUMBER_OF_MESSAGES_TO_LOAD];
        } else {
            NSLog(@"error loading account: %@", error);
        }

        strongSelf.imapCheckOp = nil;
    }];
}

- (void)loadLastNMessages:(NSUInteger)nMessages
{
    self.isLoading = YES;

    MCOIMAPMessagesRequestKind requestKind = (MCOIMAPMessagesRequestKind)
    (MCOIMAPMessagesRequestKindHeaders | MCOIMAPMessagesRequestKindStructure |
     MCOIMAPMessagesRequestKindInternalDate | MCOIMAPMessagesRequestKindHeaderSubject |
     MCOIMAPMessagesRequestKindFlags);

    NSString *inboxFolder = @"INBOX";
    MCOIMAPFolderInfoOperation *inboxFolderInfo = [self.imapSession folderInfoOperation:inboxFolder];

    [inboxFolderInfo start:^(NSError *error, MCOIMAPFolderInfo *info)
    {
        BOOL totalNumberOfMessagesDidChange =
        self.totalNumberOfInboxMessages != [info messageCount];

        self.totalNumberOfInboxMessages = [info messageCount];

        NSUInteger numberOfMessagesToLoad =
        MIN(self.totalNumberOfInboxMessages, nMessages);

        if (numberOfMessagesToLoad == 0)
        {
            self.isLoading = NO;
            return;
        }

        MCORange fetchRange;

        // If total number of messages did not change since last fetch,
        // assume nothing was deleted since our last fetch and just
        // fetch what we don't have
        if (!totalNumberOfMessagesDidChange && self.messages.count)
        {
            numberOfMessagesToLoad -= self.messages.count;

            fetchRange =
            MCORangeMake(self.totalNumberOfInboxMessages -
                         self.messages.count -
                         (numberOfMessagesToLoad - 1),
                         (numberOfMessagesToLoad - 1));
        }

        // Else just fetch the last N messages
        else
        {
            fetchRange =
            MCORangeMake(self.totalNumberOfInboxMessages -
                         (numberOfMessagesToLoad - 1),
                         (numberOfMessagesToLoad - 1));
        }

        self.imapMessagesFetchOp =
        [self.imapSession fetchMessagesByNumberOperationWithFolder:inboxFolder
                                                       requestKind:requestKind
                                                           numbers:
         [MCOIndexSet indexSetWithRange:fetchRange]];

        [self.imapMessagesFetchOp setProgress:^(unsigned int progress) {
            NSLog(@"Progress: %u of %u", progress, numberOfMessagesToLoad);
        }];

        __weak MasterViewController *weakSelf = self;
        [self.imapMessagesFetchOp start:
         ^(NSError *error, NSArray *messages, MCOIndexSet *vanishedMessages)
        {
            MasterViewController *strongSelf = weakSelf;
            NSLog(@"fetched all messages.");

            self.isLoading = NO;

            NSSortDescriptor *sort =
            [NSSortDescriptor sortDescriptorWithKey:@"header.date" ascending:NO];

            NSMutableArray *combinedMessages =
            [NSMutableArray arrayWithArray:messages];
            [combinedMessages addObjectsFromArray:strongSelf.messages];

            strongSelf.messages =
            [combinedMessages sortedArrayUsingDescriptors:@[sort]];
            [strongSelf.tableView reloadData];
        }];
    }];
}

现在我想在我的应用中集成搜索电子邮件功能。 mailcore2框架中是否有任何方法可以使用关键字获取搜索到的电子邮件。

已经尝试过该链接,但没有帮助。

iOS-Is there any method available for fetching CTCoreMessage with specific key words from CTCoreAccount or CTCoreFolder in Mailcore library?

0 个答案:

没有答案