zoukankan      html  css  js  c++  java
  • 打电话、发短信、web以及发邮件

      1 #import "ViewController.h"
      2 #import <MessageUI/MessageUI.h>   //导入信息UI库
      3 
      4 @interface ViewController () <MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>
      5 
      6 @end
      7 
      8 @implementation ViewController
      9 
     10 - (void)viewDidLoad {
     11     [super viewDidLoad];
     12     // Do any additional setup after loading the view, typically from a nib.
     13 }
     14 
     15 - (void)didReceiveMemoryWarning {
     16     [super didReceiveMemoryWarning];
     17     // Dispose of any resources that can be recreated.
     18 }
     19 
     20 - (IBAction)callPhone:(id)sender {
     21     //方式1 :拼接字符串 注意开头是tel: 这种方式打电话回不到原来应用中,会停留在通讯录里,而且是直接拨打电话 没有任何弹窗提示
     22 //    NSString *str = [NSString stringWithFormat:@"tel:%@",self.phoneTextField.text];
     23 //    //首先得到应用的单例对象 然后调用openURL:这个方法 参数是url对象
     24 //    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
     25     
     26     
     27     //方式2,这种方式有弹窗提示,并且能回到原来应用中 推荐这种
     28 //    NSString *str1 = [[NSString alloc] initWithFormat:@"tel:%@",self.phoneTextField.text];
     29 //    //创建UIWebView对象
     30 //    UIWebView *callWebView = [[UIWebView alloc] init];
     31 //    //加载一个请求对象 这个请求对象通过url对象创建  url对象又通过str1字符串获得
     32 //    [callWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str1]]];
     33 //    //加入到self.view上
     34 //    [self.view addSubview:callWebView];
     35     
     36     
     37     //方式3
     38     //这种方式 也可以有弹窗提示 并且也能回到原来的应用中  也推荐这种
     39     NSString *str2 = [NSString stringWithFormat:@"telprompt:%@",self.phoneTextField.text];
     40     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str2]];
     41     
     42 }
     43 
     44 - (IBAction)callWeb:(id)sender {
     45     
     46     //打开网址 注意:打开的网址注意是http:// 或是https://
     47     NSString *str = [NSString stringWithFormat:@"https://%@",self.webTextField.text];
     48     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
     49     
     50     
     51 }
     52 - (IBAction)sendSMS:(id)sender {
     53     
     54     //方式1:这种方式没法回到应用中,但是注意不要写中文等特殊字符 否则无法跳到发短信界面
     55     //优点:简单 缺点:不能指定发送内容,只能指定发送人,而且不能回到应用中
     56 //    NSString *str = [NSString stringWithFormat:@"sms://%@",self.smsTextField.text];
     57 //    NSURL *url = [NSURL URLWithString:str];
     58 //    [[UIApplication sharedApplication] openURL:url];
     59     
     60     
     61     //方式2 推荐这种
     62     /*
     63      优点:1.从应用出来并且能回到应用中
     64           2.可以发送多人
     65           3.可以用代码自定义消息
     66           4.如果手机开通了iMessage功能,会走网络通道,不走运营商通道
     67      */
     68     
     69     //判断用户设备是否能发送短信
     70     if (![MFMessageComposeViewController canSendText]) {
     71         NSLog(@"不能发送内容");
     72         
     73         return ;
     74     }
     75     
     76     //1.创建一个短信控制器对象
     77     MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
     78     
     79     //2.设置短信内容
     80     //  (1)收件人
     81     controller.recipients = @[@"10086",@"10010"];
     82     //  (2)短信内容
     83     controller.body = @"你好啊 你俩";
     84     //  (3)设置短信代理
     85     controller.messageComposeDelegate = self;
     86     
     87     //3.显示短信控制器
     88     
     89     [self presentViewController:controller animated:YES completion:^{
     90         NSLog(@"显示短信控制器完成代码块");
     91     }];
     92     
     93     
     94 }
     95 
     96 #pragma mark - 短信控制器代理方法
     97 
     98 - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
     99     
    100     /*
    101      MessageComposeResultCancelled, 取消
    102      MessageComposeResultSent,      发送
    103      MessageComposeResultFailed     失败
    104 
    105      result枚举
    106      */
    107     NSLog(@"%d",result);
    108     
    109     //注:别忘了回到应用中
    110     [controller dismissViewControllerAnimated:YES completion:^{
    111         NSLog(@"短信控制器消失完成后代码块");
    112     }];
    113     
    114 }
    115 
    116 - (IBAction)sendEmail:(id)sender {
    117     
    118     //方式1
    119 //    NSString *str = [NSString stringWithFormat:@"mailto://%@",self.emailTextField.text];
    120 //    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    121     
    122     //方式2
    123     //判断是否能发送邮件
    124     if (![MFMailComposeViewController canSendMail]) {
    125         NSLog(@"不能发送邮件");
    126         return;
    127     }
    128     //创建mail控制器对象
    129     MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
    130     //设置邮件主题
    131     [vc setSubject:@"我爱你"];
    132     //设置邮件发送内容 第二个参数支持HTML格式
    133     [vc setMessageBody:@"基本的电话、邮件、短信" isHTML:YES];
    134     //设置收件人列表
    135     [vc setToRecipients:@[@"*******@qq.com"]];
    136     //设置抄送人列表
    137     [vc setCcRecipients:@[@"********@qq.com",@"********@163.com"]];
    138     //设置邮件代理
    139     vc.mailComposeDelegate = self;
    140     //显示邮件控制器
    141     [self presentViewController:vc animated:YES completion:^{
    142         NSLog(@"跳转完成后执行代码块");
    143     }];
    144     
    145     
    146 }
    147 
    148 - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    149     
    150     /*
    151      result枚举类型
    152      MFMailComposeResultCancelled,  取消
    153      MFMailComposeResultSaved,      保存
    154      MFMailComposeResultSent,       发送
    155      MFMailComposeResultFailed      失败
    156      */
    157     NSLog(@"%d",result);
    158     
    159     [controller dismissViewControllerAnimated:YES completion:^{
    160         NSLog(@"邮箱控制器消失完成后代码块");
    161     }];
    162     
    163 }
    164 @end

    打电话、发短信、web以及发邮件

    另外补充一下,用下面的个人感觉更好一些!上面的UIWebView好像弹框的速度有点慢 自己弹框一下更方便一些

    - (void)callAction:(NSString *)phone{

        NSMutableString* str1=[[NSMutableString alloc]initWithString:phone];//存在堆区,可变字符串

        [str1 insertString:@"-"atIndex:3];//把一个字符串插入另一个字符串中的某一个位置

        [str1 insertString:@"-"atIndex:8];//把一个字符串插入另一个字符串中的某一个位置

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:str1 message:nil preferredStyle:UIAlertControllerStyleAlert];

        // 设置popover指向的item

        alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;

        // 添加按钮

        [alert addAction:[UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

            NSLog(@"点击了呼叫按钮");

            NSString* phoneStr = [NSString stringWithFormat:@"tel://%@",phone];

            if ([phoneStr hasPrefix:@"sms:"] || [phoneStr hasPrefix:@"tel:"]) {

                UIApplication * app = [UIApplication sharedApplication];

                if ([app canOpenURL:[NSURL URLWithString:phoneStr]]) {

                    [app openURL:[NSURL URLWithString:phoneStr]];

                }

            }

        }]];

        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

            NSLog(@"点击了取消按钮");

        }]];

            [self presentViewController:alert animated:YES completion:nil];

       }

  • 相关阅读:
    第一周。。。
    新人日报1129
    Daily Report-1126
    How to read source code[repost]
    Markdown tutorial [repost]
    蘑菇街面经
    阿里面经
    百度凤巢一二面经
    Mybatis最入门---代码自动生成(generatorConfig.xml配置)
    Maven的生命周期阶段
  • 原文地址:https://www.cnblogs.com/ljcgood66/p/5457136.html
Copyright © 2011-2022 走看看