iOS 实现发送邮件和短信

来源:互联网 发布:java中文相似度匹配 编辑:程序博客网 时间:2024/06/01 07:22

导入framework

导入MessageUI.framework

发送邮件

首先需要在.h文件中实现下面的操作:
[objc] view plaincopy
  1. #import <MessageUI/MFMailComposeViewController.h>  
然后实现下面的代理 :MFMailComposeViewControllerDelegate

[objc] view plaincopy
  1. - (void)businessContactWithMail  
  2. {  
  3.     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));  
  4.     if (mailClass != nil)  
  5.     {  
  6.         // 我们必须经常检查是否当前的设备被配置为发送电子邮件 
  7.         if ([mailClass canSendMail])  
  8.         {  
  9.             MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];  
  10.             picker.mailComposeDelegate = self;  
  11.             [picker setToRecipients:[NSArray arrayWithObject:@""]];  
  12.             [picker setSubject:[NSString stringWithFormat:@""]];  
  13.               
  14.             UIDevice *device = [UIDevice currentDevice];  
  15.               
  16.             NSString *content=[NSString stringWithFormat:@""];  
  17.             [picker setMessageBody:content isHTML:NO];  
  18.             [self presentViewController:picker animated:YES completion:^{  
  19.                   
  20.             }];  
  21.         }  
  22.         else  
  23.         {  
  24.             [NDUI showAlertWithInfo:@"您的设备尚未配置邮件账号" title:@"提示"];  
  25.         }  
  26.     } else {  
  27.         [NDUI showAlertWithInfo:@"您的设备不支持邮件功能" title:@"提示"];  
  28.     }  
  29. }  
  30. #pragma mark - Mail and SMS delegate  
  31. // 解散撰写邮件界面当用户点击“取消”或发 
  32. // 进行操作的结果更新消息字段  
  33. - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error  
  34. {  
  35.     [self dismissViewControllerAnimated:YES completion:nil];  
  36. }  


短信发送


首先需要在.h文件中实现下面的操作:
[objc] view plaincopy
  1. #import <MessageUI/MFMailComposeViewController.h>  
然后实现下面的代理 :MFMessageComposeViewControllerDelegate
[objc] view plaincopy
  1. - (void)shareWithSMS  
  2. {  
  3.     Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));  
  4.     BOOL smsEnabled=NO;  
  5.     if (messageClass != nil) {            
  6.         // 检查当前设备是否配置为发送短信  
  7.         if ([messageClass canSendText]) {  
  8.             MFMessageComposeViewController *smsClident = [[MFMessageComposeViewController alloc] init];  
  9.             smsClident.messageComposeDelegate = self;  
  10.               
  11.             NSString *messageBody = [NSString stringWithFormat:@""];  
  12.               
  13.             [smsClident setBody:messageBody];  
  14.             [self presentModalViewController:smsClident animated:YES];  
  15.         }  
  16.         else {    
  17.             [NDUI showAlertWithInfo:@"您的设备不支持此短信功能" title:@"提示"];  
  18.         }  
  19.     }  
  20.     else {  
  21.           
  22.         [NDUI showAlertWithInfo:@"您的设备不支持短信功能" title:@"提示"];  
  23.     }  
  24.     if(!smsEnabled){  
  25.         return;  
  26.     }  
  27.       
  28.       
  29. }  
  30. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result  
  31. {  
  32.     [self dismissModalViewControllerAnimated:YES];  
  33. }  
0 0
原创粉丝点击