当前位置:首页 > 操作系统 > Ios

iOS 短信分享 邮件分享

 

本地调用短信分享。

 

             1
            #import
            "
            shareViewController.h
            "
             2
             3
            @interface
             shareViewController (){

             4     UIAlertView *mfAlertview;//定义一个弹出框 5     UITextView* txYaoqingma;
 6}
 7 8@end 910@implementation shareViewController
1112 - (void)viewDidLoad {
13    [super viewDidLoad];
14// Do any additional setup after loading the view.15}
1617 - (void)didReceiveMemoryWarning {
18    [super didReceiveMemoryWarning];
19// Dispose of any resources that can be recreated.20}
2122 - (IBAction)shareButClick:(id)sender {
2324    [self showMessageViewController];
25}
2627 -(void)showMessageViewController
28{
29if( [MFMessageComposeViewController canSendText] )//判断是否能发短息30    {
3132         MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init];
33         controller.recipients = [NSArray arrayWithObject:@"10010"];//接收人,可以有很多,放入数组34         controller.body = txYaoqingma.text;//短信内容,自定义即可35         controller.messageComposeDelegate = self;//注意不是delegate3637        [self presentViewController:controller animated:YES completion:nil];
3839         [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"发送短信"];//修改短信界面标题40    }
41else42    {
4344         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"抱歉" message:@"短信功能不可用!"delegate:self cancelButtonTitle:@"" otherButtonTitles:nil, nil];
45        [alert show];
46    }
47}
4849//短信发送成功后的回调50 -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
51{
52    [controller dismissViewControllerAnimated:YES completion:nil];
5354switch (result)
55    {
56case MessageComposeResultCancelled:
57        {
58//用户取消发送59        }
60break;
61case MessageComposeResultFailed://发送短信失败62        {
63             mfAlertview=[[UIAlertView alloc]initWithTitle:@"抱歉" message:@"短信发送失败"delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
6465            [mfAlertview show];
6667        }
68break;
69case MessageComposeResultSent:
70        {
71             mfAlertview=[[UIAlertView alloc]initWithTitle:@"恭喜" message:@"短信发送成功!"delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
7273            [mfAlertview show];
7475        }
76break;
77default:
78break;
79    }
80 }

 

添加邮件分享

              1
            //
            邮件
              2
              3 -(void)showMailPicker {
  4  5     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
  6  7  8  9if (mailClass !=nil) {
 10 11if ([mailClass canSendMail]) {
 12 13            [self displayMailComposerSheet];
 14 15         }else{
 16 17             UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能"delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
 18 19            [alert show];
 20 21        }
 22 23     }else{
 24 25 26 27    }
 28 29 30 31}
 32 33 -(void)displayMailComposerSheet
 34 35{
 36 37     MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 38 39 40 41     picker.mailComposeDelegate =self;
 42 43     [picker setSubject:@"文件分享"];
 44 45// Set up recipients 46 47     NSArray *toRecipients = [NSArray arrayWithObject:@"first@qq.com"];
 48 49     NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@qq.com",@"third@qq.com", nil];
 50 51     NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@qq.com"];
 52 53 54 55    [picker setToRecipients:toRecipients];
 56 57    [picker setCcRecipients:ccRecipients];
 58 59    [picker setBccRecipients:bccRecipients];
 60 61//发送图片附件
 62 63//NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
 64 65//NSData *myData = [NSData dataWithContentsOfFile:path];
 66 67//[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy.jpg"];
 68 69//发送txt文本附件
 70 71//NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"];
 72 73//NSData *myData = [NSData dataWithContentsOfFile:path];
 74 75//[picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"];
 76 77//发送doc文本附件
 78 79//NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];
 80 81//NSData *myData = [NSData dataWithContentsOfFile:path];
 82 83//[picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"];
 84 85//发送pdf文档附件 86 87/* 88 89     NSString *path = [[NSBundlemainBundle] pathForResource:@"CodeSigningGuide"ofType:@"pdf"];
 90 91     NSData *myData = [NSDatadataWithContentsOfFile:path];
 92 93     [pickeraddAttachmentData:myData mimeType:@"file/pdf"fileName:@"rainy.pdf"];
 94 95*/ 96 97// Fill out the email body text 98 99     NSString *emailBody =[NSString stringWithFormat:@"我分享了文件给您,地址是%@",@"address"] ;
100101    [picker setMessageBody:emailBody isHTML:NO];
102103    [self presentModalViewController:picker animated:YES];
104105}
106107 - (void)mailComposeController:(MFMailComposeViewController*)controller
108109           didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
110111// Notifies users about errors associated with the interface112113switch (result)
114115    {
116117        caseMFMailComposeResultCancelled:
118119             NSLog(@"Result: Mail sending canceled");
120121break;
122123        caseMFMailComposeResultSaved:
124125             NSLog(@"Result: Mail saved");
126127break;
128129        caseMFMailComposeResultSent:
130131             NSLog(@"Result: Mail sent");
132133break;
134135        caseMFMailComposeResultFailed:
136137             NSLog(@"Result: Mail sending failed");
138139break;
140141default:
142143             NSLog(@"Result: Mail not sent");
144145break;
146147    }
148149    [self dismissModalViewControllerAnimated:YES];
150151 }

 

原文:http://www.cnblogs.com/frounk/p/4728059.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!