导入MessageUI.framework
.h文件中#import <MessageUI/MessageUI.h>
#import<MessageUI/MFMailComposeViewController.h>
实现 MFMailComposeViewControllerDelegate,
MFMessageComposeViewControllerDelegate
.m 文件
//邮件 -(void)showMailPicker { Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (mailClass !=nil) { if ([mailClass canSendMail]) { [selfdisplayMailComposerSheet]; }else{ UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [alert release]; } }else{ } } -(void)displayMailComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc] init]; picker.mailComposeDelegate =self; [pickersetSubject:@"文件分享"]; // Set up recipients NSArray *toRecipients = [NSArrayarrayWithObject:@"[email protected]"]; NSArray *ccRecipients = [NSArrayarrayWithObjects:@"[email protected]",@"[email protected]", nil]; NSArray *bccRecipients = [NSArrayarrayWithObject:@"[email protected]"]; [pickersetToRecipients:toRecipients]; [pickersetCcRecipients:ccRecipients]; [pickersetBccRecipients:bccRecipients]; //发送图片附件 //NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"]; //NSData *myData = [NSData dataWithContentsOfFile:path]; //[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy.jpg"]; //发送txt文本附件 //NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"]; //NSData *myData = [NSData dataWithContentsOfFile:path]; //[picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"]; //发送doc文本附件 //NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"]; //NSData *myData = [NSData dataWithContentsOfFile:path]; //[picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"]; //发送pdf文档附件 /* NSString *path = [[NSBundlemainBundle] pathForResource:@"CodeSigningGuide"ofType:@"pdf"]; NSData *myData = [NSDatadataWithContentsOfFile:path]; [pickeraddAttachmentData:myData mimeType:@"file/pdf"fileName:@"rainy.pdf"]; */ // Fill out the email body text NSString *emailBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address] ; [pickersetMessageBody:emailBody isHTML:NO]; [selfpresentModalViewController:picker animated:YES]; [pickerrelease]; } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { // Notifies users about errors associated with the interface switch (result) { caseMFMailComposeResultCancelled: NSLog(@"Result: Mail sending canceled"); break; caseMFMailComposeResultSaved: NSLog(@"Result: Mail saved"); break; caseMFMailComposeResultSent: NSLog(@"Result: Mail sent"); break; caseMFMailComposeResultFailed: NSLog(@"Result: Mail sending failed"); break; default: NSLog(@"Result: Mail not sent"); break; } [selfdismissModalViewControllerAnimated:YES]; }
//短信 -(void)showSMSPicker{ Class messageClass = (NSClassFromString(@"MFMessageComposeViewController")); if (messageClass != nil) { // Check whether the current device is configured for sending SMS messages if ([messageClass canSendText]) { [selfdisplaySMSComposerSheet]; } else { UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持短信功能" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [alert release]; } } else { } } -(void)displaySMSComposerSheet { MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc] init]; picker.messageComposeDelegate =self; NSString *smsBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address] ; picker.body=smsBody; [selfpresentModalViewController:picker animated:YES]; [pickerrelease]; }
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self.rootViewController dismissViewControllerAnimated:YES completion:nil]; //Notifies users about errors associated with the interface [[NSNotificationCenter defaultCenter] postNotificationName:@"SMSSendFinished" object:nil]; switch (result) { case MessageComposeResultCancelled: { UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:nil message:@"发送取消" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [msgbox show]; [msgbox release]; } break; case MessageComposeResultSent: { UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:nil message:@"发送成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [msgbox show]; [msgbox release]; } break; case MessageComposeResultFailed: { UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:nil message:@"发送失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [msgbox show]; [msgbox release]; } break; default: break; } }