博客> 打电话 发短信 写邮件
打电话 发短信 写邮件
2017-12-11 19:25 评论:0 阅读:333 beijingwang123
ios 打电话 发短信 写邮件

[font=宋体]//预约

  • (void)yuYueBtnClick:(UIButton *)sender{

    _as = [[UIActionSheet alloc] initWithTitle:@"请选择预约方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打电话",@"发短信",@"发邮件", nil]; [_as showInView:self.view];

}

  • (void)dealloc{

    [_as removeFromSuperview]; }

  • (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) {//打电话

    [self tel];

    }else if (buttonIndex == 1){//发短信

    [self sendMessage];

    }else if (buttonIndex == 2){//发邮件

    [self sendmail];

    } }

//打电话

  • (void)tel{

    if ([[UIDevice currentDevice].model isEqualToString:@"iPhone"]) { // 提示:不要将webView添加到self.view,如果添加会遮挡原有的视图 // 懒加载 if (_webView == nil) { _webView = [[UIWebView alloc] init]; }

    NSURL *url = [NSURL URLWithString:@"tel://182xxxxxxxx”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];

    } else { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"您的设备不能打电话" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil]; [alertView show];

    } }

//发送短信

  • (void)sendMessage{

    // 判断用户设备能否发送短信 if (![MFMessageComposeViewController canSendText]) {

    UIAlertView *wAlertView = [[UIAlertView alloc]initWithTitle:@"不能发送短信" message:@"请检查短信设置"delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
    [wAlertView show];
    return;

    }

    // 1. 实例化一个控制器 MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

    // 2. 设置短信内容 // 1) 收件人 controller.recipients = @[@“此处填电话号码”];

    //主题 // controller.subject = @"xxxxxxx”;

    // 2) 短信内容 // controller.body = @"给您拜个晚年,祝您晚年快乐!";

    // 3) 设置代理 controller.messageComposeDelegate = self;

    // 3. 显示短信控制器 [self presentViewController:controller animated:YES completion:nil]; }

//发送短信的代理方法

  • (void)messageComposeViewController:(MFMessageComposeViewController )controller didFinishWithResult:(MessageComposeResult)result { UIAlertView mesAlert = [[UIAlertView alloc] init];

    switch (result) { case MessageComposeResultCancelled: //取消 mesAlert.title = @"短信已被取消发送"; break; case MessageComposeResultSent: // 发送 mesAlert.title = @"短信发送成功"; break; case MessageComposeResultFailed: // 发送失败 mesAlert.title = @"短信发送失败"; break;

    default:
        mesAlert.title = @"短信尚未发送";
        break;

    }

    [mesAlert show];

    [self performSelector:@selector(dimissAlert:) withObject:mesAlert afterDelay:1.0];

}

//发邮件

  • (void)sendmail { // 1. 先判断能否发送邮件 if (![MFMailComposeViewController canSendMail]) { // 提示用户设置邮箱

    UIAlertView *wAlertView = [[UIAlertView alloc]initWithTitle:@"不能发送邮件" message:@"请检查邮件设置"delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
    [wAlertView show];
    return;

    }

    // 2. 实例化邮件控制器,准备发送邮件 MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];

    // 1) 主题 xxx的工作报告 [controller setSubject:@"我的工作报告"]; // 2) 收件人 [controller setToRecipients:@[@"xxxxxx@163.com"]];

    // 3) cc 抄送 // 4) bcc 密送(偷偷地告诉,打个小报告) // 5) 正文 // [controller setMessageBody:@"这是我的工作报告,请审阅!
    P.S. 我的头像牛X吗?" isHTML:YES];

    // 6) 附件 // UIImage image = [UIImage imageNamed:@"头像1.png"]; // NSData imageData = UIImagePNGRepresentation(image); // 1> 附件的二进制数据 // 2> MIMEType 使用什么应用程序打开附件 // 3> 收件人接收时看到的文件名称 // 可以添加多个附件 // [controller addAttachmentData:imageData mimeType:@"image/png" fileName:@"头像.png"];

    // 7) 设置代理 [controller setMailComposeDelegate:self];

    // 显示控制器 [self presentViewController:controller animated:YES completion:nil]; }

pragma mark - 邮件代理方法

  • (void)mailComposeController:(MFMailComposeViewController )controller didFinishWithResult:(MFMailComposeResult)result error:(NSError )error { UIAlertView *mesAlert = [[UIAlertView alloc] init];

    switch (result) { case MFMailComposeResultCancelled: //取消 mesAlert.title = @"邮件已被取消发送"; break; case MFMailComposeResultSent: // 发送 mesAlert.title = @"邮件发送成功"; break; case MFMailComposeResultFailed: // 发送失败 mesAlert.title = @"邮件发送失败"; break; case MFMailComposeResultSaved: //保存 mesAlert.title = @"邮件被保存了"; break; }

    [mesAlert show]; [self performSelector:@selector(dimissAlert:) withObject:mesAlert afterDelay:1.0]; }

//让警告框消失

  • (void)dimissAlert:(UIAlertView *)alert{

    [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];

    // 在面向对象程序开发中,有一个原则,谁申请,谁释放! // *** 此方法也可以正常工作,因为系统会将关闭消息发送给self // [controller dismissViewControllerAnimated:YES completion:nil];

    // 应该用这个!!! [self dismissViewControllerAnimated:YES completion:nil]; } [font=宋体]文字[/font][/font]

收藏
0
sina weixin mail 回到顶部