iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享

导入MessageUI.framework

.h文件

#import <MessageUI/MessageUI.h>

#import<MessageUI/MFMailComposeViewController.h>

 

实现

MFMailComposeViewControllerDelegate

MFMessageComposeViewControllerDelegate

 

.m 文件

  1 导入
  2 MessageUI.framework
  3 
  4 .h文件中
  5 #import <MessageUI/MessageUI.h>
  6 #import<MessageUI/MFMailComposeViewController.h>
  7 
  8 实现
  9 MFMailComposeViewControllerDelegate,
 10 MFMessageComposeViewControllerDelegate
 11 
 12 .m 文件
 13 
 14 //邮件
 15 
 16 -(void)showMailPicker
 17 {
 18     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
 19 
 20     if (mailClass !=nil)
 21     {
 22         if ([mailClass canSendMail])
 23         {
 24             [selfdisplayMailComposerSheet];
 25         }
 26         else
 27         {
 28             UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
 29             [alert show];
 30             [alert release];
 31         }
 32         
 33     }else
 34     {
 35         
 36     }
 37 
 38 }
 39 
 40 -(void)displayMailComposerSheet
 41 {
 42     MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc] init];
 43     
 44     picker.mailComposeDelegate =self;
 45     [pickersetSubject:@"文件分享"];
 46     // Set up recipients
 47     NSArray *toRecipients = [NSArrayarrayWithObject:@"first@qq.com"];
 48     NSArray *ccRecipients = [NSArrayarrayWithObjects:@"second@qq.com",@"third@qq.com", nil];
 49     NSArray *bccRecipients = [NSArrayarrayWithObject:@"fourth@qq.com"];
 50 
 51     [pickersetToRecipients:toRecipients];
 52     [pickersetCcRecipients:ccRecipients];
 53     [pickersetBccRecipients:bccRecipients];
 54     
 55     //发送图片附件
 56     //NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
 57     //NSData *myData = [NSData dataWithContentsOfFile:path];
 58     //[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy.jpg"];
 59     
 60     //发送txt文本附件
 61     //NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"];
 62     //NSData *myData = [NSData dataWithContentsOfFile:path];
 63     //[picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"];
 64     
 65     //发送doc文本附件
 66     //NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];
 67     //NSData *myData = [NSData dataWithContentsOfFile:path];
 68     //[picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"];
 69     //发送pdf文档附件
 70     
 71     /*
 72      NSString *path = [[NSBundlemainBundle] pathForResource:@"CodeSigningGuide"ofType:@"pdf"];
 73      NSData *myData = [NSDatadataWithContentsOfFile:path];
 74      [pickeraddAttachmentData:myData mimeType:@"file/pdf"fileName:@"rainy.pdf"];
 75      */
 76     
 77     // Fill out the email body text
 78     NSString *emailBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address] ;
 79     [pickersetMessageBody:emailBody isHTML:NO];
 80     [selfpresentModalViewController:picker animated:YES];
 81     [pickerrelease];
 82 }
 83 
 84 - (void)mailComposeController:(MFMailComposeViewController*)controller
 85 didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
 86 {
 87     // Notifies users about errors associated with the interface
 88     switch (result)
 89     
 90     {
 91             
 92         case MFMailComposeResultCancelled:
 93             NSLog(@"Result: Mail sending canceled");
 94             break;
 95             
 96         case MFMailComposeResultSaved:
 97             NSLog(@"Result: Mail saved");
 98             break;
 99             
100         case MFMailComposeResultSent:
101             NSLog(@"Result: Mail sent");
102             break;
103             
104         case MFMailComposeResultFailed:
105             NSLog(@"Result: Mail sending failed");
106             break;
107             
108         default:
109             NSLog(@"Result: Mail not sent");
110             break;
111     }
112     
113     [selfdismissModalViewControllerAnimated:YES];
114 }
115 
116 //短信
117 
118 -(void)showSMSPicker
119 {
120     Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
121 
122     if (messageClass != nil)
123     {
124         // Check whether the current device is configured for sending SMS messages
125         if ([messageClass canSendText])
126         {
127             [selfdisplaySMSComposerSheet];
128         }
129         else
130         {
131             UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持短信功能" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
132             [alert show];
133             [alert release];
134         }
135     }
136     else
137     {
138     }
139 }
140 
141 -(void)displaySMSComposerSheet
142 {
143     MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc] init];
144     picker.messageComposeDelegate =self;
145     NSString *smsBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address] ;
146     picker.body=smsBody;
147     [selfpresentModalViewController:picker animated:YES];
148     [pickerrelease];
149 }
View Code

转自:http://blog.csdn.net/shijiucdy/article/details/7364625

原文地址:https://www.cnblogs.com/qc0815/p/3401115.html