查看: 2213|回复: 0

[IOS开发教程] iOS打电话、发短信、发邮件实例代码

发表于 2017-12-27 08:00:00

本文实例为大家分享了iOS打电话、发短信、发邮件,指定QQ用户发送QQ消息的具体代码,供大家参考,具体内容如下

1.发短信

头文件

  1. #import <MessageUI/MessageUI.h>
复制代码

头部代理

  1. @interface ViewController ()<MFMessageComposeViewControllerDelegate>
复制代码

发送信息

  1. - (void)senderMessage{
  2. if([MFMessageComposeViewController canSendText]) {
  3. ///短信实例化
  4. MFMessageComposeViewController * messageVc = [[MFMessageComposeViewController alloc] init];
  5. ///目标号码,可以多个
  6. messageVc.recipients = @[@"18888888888",@"15555555555"];
  7. messageVc.navigationBar.tintColor = [UIColor redColor];
  8. messageVc.body = @"发送的内容-发送的内容-发送的内容";
  9. messageVc.messageComposeDelegate = self;
  10. [self presentViewController:messageVc animated:YES completion:nil];
  11. [[[[messageVc viewControllers] lastObject] navigationItem] setTitle:@"title"];
  12. }
  13. else {
  14. NSLog(@"当前设备不支持短信功能");
  15. }
  16. }
复制代码

代理方法实现

  1. -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
  2. ///dismiss MFMessageComposeViewController
  3. [controller dismissViewControllerAnimated:YES completion:nil];
  4. if (result == MessageComposeResultSent) {
  5. NSLog(@"信息发送成功!");
  6. }
  7. else if (result == MessageComposeResultFailed){
  8. NSLog(@"信息发送失败!");
  9. }
  10. else if (result == MessageComposeResultCancelled){
  11. NSLog(@"点击了取消");
  12. }
  13. else{
  14. }
  15. }
复制代码

2.拨打电话

  1. - (void)dialPhone{
  2. ///该方法直接拨打,不会弹出呼叫(取消)
  3. [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://18888888888"]];
  4. ///该方法会弹出是否取消呼叫
  5. UIWebView*callWebview =[[UIWebView alloc] init];
  6. NSURL *telURL =[NSURL URLWithString:@"tel:18888888888"];
  7. [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
  8. [self.view addSubview:callWebview];
  9. }
复制代码

3.发送邮件

  1. - (void)senderEmail{
  2. NSString *urlStr =@"mailto:xxxxxxxx@xxxx.com?subject=邮件主题(此参数可不传)&body=邮件内容(此参数可不传)";
  3. NSURL *url = [NSURL URLWithString:urlStr] ;
  4. [[UIApplication sharedApplication] openURL:url];
  5. }
复制代码

4.发送QQ消息

  1. - (void)senderQQ{
  2. ///111111111111为QQ号码
  3. NSString *url = @"mqq://im/chat?chat_type=wpa&uin=111111111111&version=1&src_type=web";
  4. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  5. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报