OC_调用系统短信_邮件_电话

来源:互联网 发布:网络攻防实验报告 编辑:程序博客网 时间:2024/06/03 17:32
//我们应用中也是常常会用到这些应用,这里说一些常用且好理解的方法!#import "NewViewController.h"#import <MessageUI/MessageUI.h>@interface NewViewController ()<MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate>//邮件代理  短信代理 这两个主要是用户操作回调@end@implementation NewViewController- (void)viewDidLoad {    [super viewDidLoad];        NSArray *arr = @[@"电话",@"短信",@"邮件"];    for(int i = 0;i<6;i++)    {        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];        btn.frame = CGRectMake(10+i%3*100, 80+i/3*150, 100, 60);        [btn setTitle:arr[i%3] forState:UIControlStateNormal];        [self.view addSubview:btn];        btn.tag = 1000+i;        [btn addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchUpInside];    }        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 320, 40)];    lab.text = @"我上面是直接跳转打开,下面更加高级一点的支持预定义";    [self.view addSubview:lab];}-(void)btnDown:(UIButton*)btn{    switch (btn.tag) {        case 1000:        {//关键字:tel  直接拨打电话,不会有任何提示            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://13811111234"]];            break;        }        case 1001:        {//关键字:sms  收件人直接为13811111234,无短信内容            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://13811111234"]];            break;        }        case 1002:        {//关键字:mail  定义好了邮件接受者,无任何内容            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://mail@qq.com"]];            break;        }                                case 1003:        {//通过webView调用 会有一个系统的提示确认是否呼叫            NSURL *phoneURL             = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",@"13811111234"]];            UIWebView *phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];            [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];            [self.view addSubview:phoneCallWebView];            break;        }        case 1004:        {            //实例化messageController,可以预先定义发送短信内容            MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];                        if([MFMessageComposeViewController canSendText])            {//判断设备能否实现发短信功能                                controller.body                   = @"恭喜您!春天种了一个苹果表,秋天收到一个破苹果表";                controller.recipients             = @[@"10010",@"10086"];                 //短信接收对象                controller.messageComposeDelegate = self;                                 //短信功能回调                [self presentViewController:controller animated:YES completion:nil];            }            break;        }        case 1005:        {            NSData *imageData                       = UIImagePNGRepresentation([UIImage imageNamed: @"icon.png"]);            MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init]; //实例化邮件VC            mailPicker.mailComposeDelegate          = self;<pre name="code" class="objc">            [mailPicker setSubject: @"我是主题,主题,主,题....."];                                   //设置主题            [mailPicker setToRecipients: @[@"second@example.com",@"third@example.com"]];         // 添加发送者,是个数组            [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"icon.png"];       // 添加图片            [mailPicker setMessageBody:@"我是邮件的正文" isHTML:YES];                              //添加正文            [self presentViewController:mailPicker animated:YES completion:nil];                //将mailController弹出            break;        }        default:            break;    }}/** *  邮件发送后回调 * *  @param controller 视图控制器对象管理邮件组合视图。 *  @param result     用户行为返回结果 *  @param error      如果出现错误,会包含错误对象和错误信息 */- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{    NSString *msg;    switch (result)    {        case MFMailComposeResultCancelled:            msg = @"邮件发送取消";            break;        case MFMailComposeResultSaved:            msg = @"邮件保存成功";            break;        case MFMailComposeResultSent:            msg = @"邮件发送成功";            break;        case MFMailComposeResultFailed:            msg = @"邮件发送失败";            break;        default:            break;    }    [self dismissViewControllerAnimated:YES completion:nil];}/** *  短信行为回调 * *  @param controller  视图控制器对象管理邮件组合视图。 *  @param result     用户返回结果 */- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{    [self dismissViewControllerAnimated:YES completion:nil];//将messageController移除        if (result == MessageComposeResultCancelled)//返回值为取消    {        NSLog(@"Message cancelled");    }    else if (result == MessageComposeResultSent)//返回值为发出    {        NSLog(@"Message sent");    }    else//其他问题    {        NSLog(@"Message failed");    }}@end

0 0