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:nilcancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
[controller dismissModalViewControllerAnimated:YES];
}
Special Thanks for Mobile and Social NEtwork Technology.
Dinesh Sharma
No comments:
Post a Comment