Search code examples
iphonesdkmailcore

MailCore IMAP Server Causing App to Crash?


I am using MailCore to build my email client and ran into a little problem while try to receive messages from the email account using IMAP.

Here is the code I have in my viewDidLoad for this:

CTCoreAccount *account = [[CTCoreAccount alloc] init];
        BOOL success = [account connectToServer:@"imap.mail.me.com"
                                           port:993
                                 connectionType:CTConnectionTypePlain
                                       authType:CTImapAuthTypePlain
                                          login:[keychain objectForKey:(__bridge id)kSecAttrAccount]
                                       password:[keychain objectForKey:(__bridge id)kSecValueData]];
        if (!success) {

            UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"Error Checking Email" message:@"There was a problem checking your inbox, please try again later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView1 show];

        }


        CTCoreFolder *inbox = [account folderWithPath:@"INBOX"];
        messages = [inbox messagesFromSequenceNumber:1 to:0 withFetchAttributes:CTFetchAttrEnvelope];
        [tableView reloadData];

The problem is when I run my app, it doesn't launch and my phone shows blank black screen. I tried commenting out the code, and everything works.

Thanks!


Solution

  • That blocks the main (UI) thread connecting and downloading messages, which can easily take longer than the time allowed to launch your app (about 20 seconds, I think).

    It looks like MailCore does not have an asynchronous API, so you'll have to use it in the background yourself. I recommend using dispatch queues (dispatch_async() is reasonably straightforward to use correctly provided you only use weak references to UIKit classes) or NSOperationQueue to make things run in the background.

    The usual concurrency warnings apply.