iOS 短信分享 邮件分享

时间:2022-12-14 05:06:01

本地调用短信分享。

 #import "shareViewController.h"

 @interface shareViewController (){
UIAlertView *mfAlertview;//定义一个弹出框
UITextView* txYaoqingma;
} @end @implementation shareViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)shareButClick:(id)sender { [self showMessageViewController];
} -(void)showMessageViewController
{
if( [MFMessageComposeViewController canSendText] )//判断是否能发短息
{ MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init];
controller.recipients = [NSArray arrayWithObject:@""];//接收人,可以有很多,放入数组
controller.body = txYaoqingma.text;//短信内容,自定义即可
controller.messageComposeDelegate = self;//注意不是delegate [self presentViewController:controller animated:YES completion:nil]; [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"发送短信"];//修改短信界面标题
}
else
{ UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"抱歉" message:@"短信功能不可用!" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
[alert show];
}
} //短信发送成功后的回调
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[controller dismissViewControllerAnimated:YES completion:nil]; switch (result)
{
case MessageComposeResultCancelled:
{
//用户取消发送
}
break;
case MessageComposeResultFailed://发送短信失败
{
mfAlertview=[[UIAlertView alloc]initWithTitle:@"抱歉" message:@"短信发送失败" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil]; [mfAlertview show]; }
break;
case MessageComposeResultSent:
{
mfAlertview=[[UIAlertView alloc]initWithTitle:@"恭喜" message:@"短信发送成功!" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil]; [mfAlertview show]; }
break;
default:
break;
}
}

添加邮件分享

 //邮件

 -(void)showMailPicker {

     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

     if (mailClass !=nil) {

         if ([mailClass canSendMail]) {

             [self displayMailComposerSheet];

         }else{

             UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];

             [alert show];

         }

     }else{

     }

 }

 -(void)displayMailComposerSheet

 {

     MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

     picker.mailComposeDelegate =self;

     [picker setSubject:@"文件分享"];

     // Set up recipients

     NSArray *toRecipients = [NSArray arrayWithObject:@"first@qq.com"];

     NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@qq.com",@"third@qq.com", nil];

     NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@qq.com"];

     [picker setToRecipients:toRecipients];

     [picker setCcRecipients:ccRecipients];

     [picker setBccRecipients: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 =[NSString stringWithFormat:@"我分享了文件给您,地址是%@",@"address"] ;

     [picker setMessageBody:emailBody isHTML:NO];

     [self presentModalViewController:picker animated:YES];

 }

 - (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;

     }

     [self dismissModalViewControllerAnimated:YES];

 }