美文网首页iOS程序员的业余沙龙
iOS 拨打电话/发送短信和邮件

iOS 拨打电话/发送短信和邮件

作者: 90de46ea2b08 | 来源:发表于2017-02-27 11:10 被阅读86次

    拨打电话

    • 遵守协议:UIAlertViewDelegate
    • 拨打电话
    UIAlertView *alt=[[UIAlertView alloc] initWithTitle:nil message:@“18211453217” delegate:self cancelButtonTitle:@"呼叫" otherButtonTitles:@"取消", nil];
    [alt show];
    
    • 实现协议UIAlertViewDelegate
    //拨打电话UIAlertViewDelegate
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    if (buttonIndex == 0) {
    NSString *str = [NSString stringWithFormat:@"tel://%@",alertView.message];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }else{
    }
    }
    

    发送短信

    • 导入头文件 #import <MessageUI/MessageUI.h>
    • 遵守协议:UIAlertViewDelegate,MFMessageComposeViewControllerDelegate
    • 发送短信
    UIAlertView *alt=[[UIAlertView alloc] initWithTitle: initWithTitle:nil message:@“18287671298” delegate:self cancelButtonTitle:@“发送短信” otherButtonTitles:@"取消", nil];
    [alt show];
    
    • 实现代理方法UIAlertViewDelegate
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0) {
            //发短信
            [self showMessageView:@[alertView.message,@"18010155927"] body:@""];
        }else{
        }
    }
    
    • 实现代理方法MFMessageComposeViewControllerDelegate
    -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        switch (result) {
            case MessageComposeResultSent:
            //信息传送成功
            break;
            case MessageComposeResultFailed:
            //信息传送失败
            break;
    case MessageComposeResultCancelled:
    //信息被用户取消传送
            break;
            default:
            break;
        }
    }
    
    • 发送短信的方法
    -(void)showMessageView:(NSArray *)phones body:(NSString *)body
    {
        if( [MFMessageComposeViewController canSendText] )
        {
            MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
            controller.recipients = phones;
            controller.navigationBar.tintColor = [UIColor redColor];
            controller.body = body;
            controller.messageComposeDelegate = self;
            [self presentViewController:controller animated:YES completion:nil];
        }else{
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"该设备不支持短信功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    

    发送邮件

    • 遵守协议:UIAlertViewDelegate
    • 发送邮件
    UIAlertView *alt=[[UIAlertView alloc] initWithTitle:@"发送邮件" message:@"123@163.com” delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alt show];
    
    • 发送邮件UIAlertViewDelegate
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {    
        if (buttonIndex == 0) { 
               [self launchMailApp:alertView.message];   
         }else{
    
        }
    }
    
    • pragma mark - 使用系统邮件客户端发送邮件

    -(void)launchMailApp:(NSString *)emailStr{
         NSMutableString *mailUrl = [[NSMutableString alloc]init];    
        //添加收件人   
         NSArray *toRecipients = [NSArray arrayWithObject: emailStr]; 
         [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];  
        //添加抄送
      //    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
      //    [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];   
       //添加密送
      //    NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
      //    [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]]; 
          //添加主题    
          [mailUrl appendString:@"&subject=my email"];  
        //添加邮件内容  
        [mailUrl appendString:@"&body=emailbody!"]; 
         NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];   
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
    }
    

    相关文章

      网友评论

      本文标题:iOS 拨打电话/发送短信和邮件

      本文链接:https://www.haomeiwen.com/subject/tshzwttx.html