zoukankan      html  css  js  c++  java
  • iOS 打电话 发邮件

      // 打电话
            // 弊端:使用该方法进行拨号之后,当电话挂断之后不会返回应用程序, 会停留在通话记录界面
    //        NSURL *url = [NSURL URLWithString:@"tel://13261936021"];
    //        [[UIApplication sharedApplication] openURL:url];
           
            // 在拨打电话之后会提示用户是否拨打, 当电话挂断之后会返回应用程序
           
            if (_webView == nil) {
                _webView = [[UIWebView alloc] initWithFrame:CGRectZero];
            }
            [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://13261936021"]]];
        };
     
    #import <MessageUI/MessageUI.h>
      // 如果利用该方式发送短信, 当短信发送完毕或者取消之后不会返回应用程序
    //        NSURL *url = [NSURL URLWithString:@"sms://10010"];
    //        [[UIApplication sharedApplication] openURL:url];
           
            // 判断当前设备能否发送短信
            if (![MFMessageComposeViewController canSendText]) {
                NSLog(@"当前设备不能发送短信");
                return ;
            }
           
            MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
            // 设置短信内容
            vc.body = @"吃饭了没?";
            // 设置收件人列表
            vc.recipients = @[@"10010"];
            // 设置代理
            vc.messageComposeDelegate = self;
            // 显示控制器
            [self presentViewController:vc animated:YES completion:nil];

        };
       
      // 当邮件发送成功或者失败或者取消之后不会回到原来的应用程序
    //        NSURL *url = [NSURL URLWithString:@"mailto://10010@qq.com"];
    //        [[UIApplication sharedApplication] openURL:url];

           
            // 不能发邮件
            if (![MFMailComposeViewController canSendMail]) return;
           
            // 当邮件发送成功或者失败或者取消之后会回到原始程序
            MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
           
            // 设置邮件主题
            [vc setSubject:@"会议"];
            // 设置邮件内容
            [vc setMessageBody:@"今天下午开会吧" isHTML:NO];
            // 设置收件人列表
            [vc setToRecipients:@[@"643055866@qq.com"]];
            // 设置抄送人列表
            [vc setCcRecipients:@[@"1234@qq.com"]];
            // 设置密送人列表
            [vc setBccRecipients:@[@"56789@qq.com"]];
           
            // 添加附件(一张图片)
            UIImage *image = [UIImage imageNamed:@"lufy.jpeg"];
            NSData *data = UIImageJPEGRepresentation(image, 0.5);
            [vc addAttachmentData:data mimeType:@"image/jepg" fileName:@"lufy.jpeg"];
           
            // 设置代理
            vc.mailComposeDelegate = self;
            // 显示控制器
            [self presentViewController:vc animated:YES completion:nil];

           
        };
    发短信
    MessageUI.framework,入下图
    1 //发送短信的方法
     2 -(void)sendMessage
     3 {
     4     //用于判断是否有发送短信的功能(模拟器上就没有短信功能)
     5     Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
     6     
     7     //判断是否有短信功能
     8     if (messageClass != nil) {
     9           //有发送功能要做的事情
    10     }
    11     else
    12     {
    13         
    14          UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"iOS版本过低(iOS4.0以后)" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
    15         
    16         [alterView show];
    17     }
    18     
    19     
    20 }

    (2).如果有发送短信功能的话,就得判断iOS版本释放支持"MFMessageComposeViewController". iOS4.0之后支持

     1     //有短信功能
     2         if ([messageClass canSendText]) {
     3               //发送短信
     4         }
     5         else
     6         {
     7             UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该设备没有发送短信的功能~" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
     8             
     9             [alterView show];
    10         }
    11
    

    (3)、经过各种验证后确定设备可以使用MFMessageComposeViewController,我们就开始用了

     1          //实例化MFMessageComposeViewController,并设置委托
     2             MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
     3             messageController.delegate = self;
     4             
     5             
     6             //拼接并设置短信内容
     7             NSString *messageContent = [NSString stringWithFormat:@"亲爱的,这个是专属属你我应用的邀请码:%@",self.authCodeLabel.text];
     8             messageController.body = messageContent;
     9             
    10             //设置发送给谁
    11             messageController.recipients = @[self.phoneNumberTextField.text];
    12             
    13             //推到发送试图控制器
    14             [self presentViewController:messageController animated:YES completion:^{
    15                 
    16             }];
    

    (4),差点给忘了,实现相应的委托回调协议是少不了的~要实现MFMessageComposeViewControllerDelegate,UINavigationControllerDelegate这两个协议。发送后的回调如下:

     1 //发送短信后回调的方法
     2 -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
     3 {
     4     NSString *tipContent;
     5     switch (result) {
     6         case MessageComposeResultCancelled:
     7             tipContent = @"发送短信已";
     8             break;
     9         
    10         case MessageComposeResultFailed:
    11             tipContent = @"发送短信失败";
    12             break;
    13         
    14         case MessageComposeResultSent:
    15             tipContent = @"发送成功";
    16             break;
    17             
    18         default:
    19             break;
    20     }
    21     
    22     UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:tipContent delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
    23     [alterView show];
    24 }
    
     
    一天一章
  • 相关阅读:
    HTML连载10-details标签&summary标签&marquee标签
    [刷题] 7-14 然后是几点
    [刷题] 7-18 出租车计价 (15 分)
    [刷题] PTA 7-20 简单计算器
    [刷题] PTA 7-22 用天平找小球
    [刷题] PTA 7-24 猜数字游戏
    [刷题] PTA 7-28 求整数的位数及各位数字之和
    [刷题] PTA 7-30 念数字
    [刷题] PTA 7-37 输出整数各位数字
    [刷题] PTA 7-35 猴子吃桃问题
  • 原文地址:https://www.cnblogs.com/hangman/p/5367162.html
Copyright © 2011-2022 走看看