iOS 短信分享 邮件分享

 

本地调用短信分享。

 

 1 #import "shareViewController.h"
 2 
 3 @interface shareViewController (){
 4     UIAlertView *mfAlertview;//定义一个弹出框
 5     UITextView* txYaoqingma;
 6 }
 7 
 8 @end
 9 
10 @implementation shareViewController
11 
12 - (void)viewDidLoad {
13     [super viewDidLoad];
14     // Do any additional setup after loading the view.
15 }
16 
17 - (void)didReceiveMemoryWarning {
18     [super didReceiveMemoryWarning];
19     // Dispose of any resources that can be recreated.
20 }
21 
22 - (IBAction)shareButClick:(id)sender {
23     
24     [self showMessageViewController];
25 }
26 
27 -(void)showMessageViewController
28 {
29     if( [MFMessageComposeViewController canSendText] )//判断是否能发短息
30     {
31         
32         MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init];
33         controller.recipients = [NSArray arrayWithObject:@"10010"];//接收人,可以有很多,放入数组
34         controller.body = txYaoqingma.text;//短信内容,自定义即可
35         controller.messageComposeDelegate = self;//注意不是delegate
36         
37         [self presentViewController:controller animated:YES completion:nil];
38         
39         [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"发送短信"];//修改短信界面标题
40     }
41     else
42     {
43         
44         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"抱歉" message:@"短信功能不可用!" delegate:self cancelButtonTitle:@"" otherButtonTitles:nil, nil];
45         [alert show];
46     }
47 }
48 
49 //短信发送成功后的回调
50 -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
51 {
52     [controller dismissViewControllerAnimated:YES completion:nil];
53     
54     switch (result)
55     {
56         case MessageComposeResultCancelled:
57         {
58             //用户取消发送
59         }
60             break;
61         case MessageComposeResultFailed://发送短信失败
62         {
63             mfAlertview=[[UIAlertView alloc]initWithTitle:@"抱歉" message:@"短信发送失败" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
64             
65             [mfAlertview show];
66             
67         }
68             break;
69         case MessageComposeResultSent:
70         {
71             mfAlertview=[[UIAlertView alloc]initWithTitle:@"恭喜" message:@"短信发送成功!" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
72             
73             [mfAlertview show];
74             
75         }
76             break;
77         default:
78             break;
79     }
80 }

 

添加邮件分享

  1 //邮件
  2 
  3 -(void)showMailPicker {
  4     
  5     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
  6     
  7     
  8     
  9     if (mailClass !=nil) {
 10         
 11         if ([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"] ;
100     
101     [picker setMessageBody:emailBody isHTML:NO];
102     
103     [self presentModalViewController:picker animated:YES];
104     
105 }
106 
107 - (void)mailComposeController:(MFMailComposeViewController*)controller
108 
109           didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
110     
111     // Notifies users about errors associated with the interface
112     
113     switch (result)
114     
115     {
116             
117         caseMFMailComposeResultCancelled:
118             
119             NSLog(@"Result: Mail sending canceled");
120             
121             break;
122             
123         caseMFMailComposeResultSaved:
124             
125             NSLog(@"Result: Mail saved");
126             
127             break;
128             
129         caseMFMailComposeResultSent:
130             
131             NSLog(@"Result: Mail sent");
132             
133             break;
134             
135         caseMFMailComposeResultFailed:
136             
137             NSLog(@"Result: Mail sending failed");
138             
139             break;
140             
141         default:
142             
143             NSLog(@"Result: Mail not sent");
144             
145             break;
146             
147     }
148     
149     [self dismissModalViewControllerAnimated:YES];
150     
151 }

 

转载于:https://www.cnblogs.com/frounk/p/4728059.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值