Search code examples
iosobjective-cipadios7mailcore

Unable to download image and PDF from mail successfully


I'm new to IOS development and i have a problem in downloading my attachments from mail.when i'm trying to download image or PDF attachments from my mail to documents directory using MailCore2 SDK it is downloading sucessfully.but my problem is that the downloaded image or PDF displays nothing (i.e.,).It gives me the file but with a empty file with zero bytes.im able to know that problem is with the NSData what im using to writeToFile is incorrect.But im unable to solve it.please help me.

Here is the code i'm using to download file from mail

MCOIMAPFetchContentOperation * op = [self.imapSession fetchMessageByUIDOperationWithFolder:@"INBOX" uid:[sender tag]];

[op start:^(NSError * error, NSData * data) {
    if ([error code] != MCOErrorNone) {
        return;
    }
    NSAssert(data != nil, @"data != nil");

    MCOMessageParser * msg = [MCOMessageParser messageParserWithData:data];

    if ([msg.attachments count] > 0)
    {
        MCOIMAPPart *part = [msg.attachments objectAtIndex:0];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *saveDirectory = [paths objectAtIndex:0];
        NSLog(@"%@",part.filename);

        NSString *attachmentPath = [[saveDirectory stringByAppendingString:@"/Downloads"] stringByAppendingPathComponent:part.filename];

        if (fileExists) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"
                                                            message:@"Fail Existed in Download Path."
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else{

            [msg.data writeToFile:attachmentPath atomically:YES];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"
                                                            message:@"Download Success /n Saved in Downloads"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        [self.tableView reloadData];
    }
}];

please help me to get out of this problem and tell me where am i doing mistake.

Thanks in advance.


Solution

  • I used like this:

    MCOIMAPFetchContentOperation * op = [self.imapSession fetchMessageByUIDOperationWithFolder:@"INBOX" uid:[sender tag]];
    
    [op start:^(NSError * error, NSData * data)
    {
        if ([error code] != MCOErrorNone)
        {
            return;
        }
        
        NSAssert(data != nil, @"data != nil");
        
        MCOMessageParser * msg = [MCOMessageParser messageParserWithData:data];
        
        if ([msg.attachments count] > 0)
        {
            MCOAttachment *attachment = [msg.attachments objectAtIndex:0];
            
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            
            NSString *saveDirectory = [paths objectAtIndex:0];
            NSLog(@"%@",attachment.filename);
            
            NSString *downLoadDirectory = [saveDirectory stringByAppendingString:@"/Downloads"];
            
            if (![[NSFileManager defaultManager] fileExistsAtPath:downLoadDirectory])
            {
                [[NSFileManager defaultManager] createDirectoryAtPath:downLoadDirectory withIntermediateDirectories:YES attributes:nil error:nil];
            }
    
            NSString *attachmentPath = [downLoadDirectory stringByAppendingPathComponent:attachment.filename];
            
            if ([[NSFileManager defaultManager] fileExistsAtPath:attachmentPath])
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"
                                                                message:@"Fail Existed in Download Path."
                                                               delegate:self
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
                [alert show];
            }
            else
            {
                BOOL res = [[attachment data] writeToFile:attachmentPath atomically:YES];
                
                if (res)
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"
                                                                    message:@"Download Success /n Saved in Downloads"
                                                                   delegate:self
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles:nil];
                    [alert show];
                }
            }
        }
    }];

    I hope they are useful.