Wednesday, February 16, 2011

Full Proof Email Composer in iPhone .

This is a full proof method to embed email interface in your app. It handles the scenario when you don't have email feature on your device or you don't have any email account set up on your device.
This piece of code also shows you how to attach pdfs as well.




UIButton *emailPdfButton=[[UIButton alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
[emailPdfButton setBackgroundColor:[UIColor greenColor]];
[emailPdfButton addTarget:self action:@selector(emailPdf:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:emailPdfButton];



-(IBAction)emailPdf:(id)sender{
    
    //find if email exists
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {        
        //find atleast one email account is set up
        if([MFMailComposeViewController canSendMail])
        {
            MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[controller setMessageBody:nil isHTML:YES];
[controller setSubject:@"Subject."];
[controller setMessageBody:@"Body Of Email." isHTML:NO];
[controller setToRecipients:[NSArray arrayWithObjects:nil,nil]];
[controller addAttachmentData:[NSData dataWithContentsOfFile:[self GetPdfFilePath]] mimeType:@"application/pdf" fileName:@"file.pdf"];
controller.navigationBar.tintColor = [UIColor grayColor];
[controller setMailComposeDelegate:self];
[self presentModalViewController:controller animated:YES];
[controller release];
        
        }
        else
        {
            //display the alert that set up an email account
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please set up your email account on device." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
            [alertView release];
        }
    }
    else
    {
        //display the alert that email account not available
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No valid email account available on device." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    
    }
    
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    
    
    
    NSString *message = @"";
    switch (result) {
         
        case MFMailComposeResultCancelled:
            message = @"Email Canceled";
            break;
        case MFMailComposeResultSaved:
            message = @"Email Saved";
            break;
        case MFMailComposeResultSent:
            message = @"Email Sent";
            break;
        case MFMailComposeResultFailed:
            message = @"Email Failed";
            break;
        default:
            message = @"Email Not Sent";
            break;

            
    }
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
    [controller dismissModalViewControllerAnimated:YES];
}



No comments:

Post a Comment