日常开发中,我们都会拥有打电话,发短信的需求,那么本文就简单讲解一下如何实现打电话和发短信

# 一、打电话

# 1. 使用 tel 协议拨打电话

   //方式1 最简单最直接的方式:直接跳到拨号界面 电话打完后,不会自动回到原应用,直接停留在通话记录界面
    //tel://
NSURL *url = [NSURL URLWithString:@"tel://10000"];

[[UIApplication sharedApplication] openURL:url];

dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://111111"] options:@{} completionHandler:nil];
});

# 2.使用 webview 拨打电话


    //方式2 WebView方式 创建一个UIWebView来加载URL,拨完后能自动回到原应用 这个webView千万不要添加到界面上来,不然会挡住其他界面

   self.webView = [[WKWebView alloc] initWithFrame:CGRectZero];

   [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10000"]]];

# 二、发短信

发短信也有两种方式

  • 使用 tel 协议
  • 使用 MessageUI 框架

# 1. 使用 sms 协议拨打电话

//直接跳到发短信界面,但是不能指定短信内容,而且不能自动回到原应用
//sms://
NSURL *url = [NSURL URLWithString:@"sms://10000"];
[[UIApplication sharedApplication] openURL:url];

# 2. 使用 MessageUI 框架

  • 项目需要导入 MessageUI.framework 框架
  • 在对应类里导入头文件:#import <MessageUI/MessageUI.h>
- (IBAction)message:(id)sender {

    //方式2 如果想指定短信内容,那就得使用MessageUI框架

    if([MFMessageComposeViewController canSendText]) {

        self.vc = [[MFMessageComposeViewController alloc] init];
        // 设置短信内容
        self.vc.body = @"吃饭了没?";
        // 设置收件人列表
        self.vc.recipients = @[@"10010", @"02010010"];
        // 设置代理
        self.vc.messageComposeDelegate = self;

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

    else{

        NSLog(@"不支持");

    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{

    // 关闭短信界面

    [controller dismissViewControllerAnimated:YES completion:nil];

    if (result == MessageComposeResultCancelled) {

        NSLog(@"取消发送");

    } else if (result == MessageComposeResultSent) {

        NSLog(@"已经发出");

    } else {

        NSLog(@"发送失败");

    }

}

# 三、发邮件

# 1. 使用 openURL

- (IBAction)email:(id)sender {

        //创建可变的地址字符串对象
        NSMutableString *mailUrl = [[NSMutableString alloc] init];
        //添加收件人,如有多个收件人,可以使用componentsJoinedByString方法连接,连接符为","
        NSString *recipients = @"gaogangwork@qq.com";
        [mailUrl appendFormat:@"mailto:%@?", recipients];
        //添加抄送人
        NSString *ccRecipients = @"gaogangwork@qq.com";
        [mailUrl appendFormat:@"&cc=%@", ccRecipients];
        //添加密送人
        NSString *bccRecipients = @"gaogangwork@163.com";
        [mailUrl appendFormat:@"&bcc=%@", bccRecipients];
        //添加邮件主题
        [mailUrl appendFormat:@"&subject=%@",@"设置邮件主题"];
        //添加邮件内容
        [mailUrl appendString:@"&body=<b>Hello</b> World!"];
        //跳转到系统邮件App发送邮件
        NSString *emailPath = [mailUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:emailPath] options:@{} completionHandler:nil];

}

# 2. 使用 MFMailComposeViewController

  • 项目需要导入 MessageUI.framework 框架
  • 在对应类里导入头文件:#import <MessageUI/MessageUI.h>
  • 对应的类遵从代理:MFMailComposeViewControllerDelegate
//判断用户是否已设置邮件账户
if ([MFMailComposeViewController canSendMail]) {
       [self sendEmailAction]; // 调用发送邮件的代码
}else{
       //给出提示,设备未开启邮件服务
}

-(void)sendEmailAction{
             // 创建邮件发送界面
    MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
            // 设置邮件代理
    [mailCompose setMailComposeDelegate:self];
            // 设置收件人
    [mailCompose setToRecipients:@[@"gaogangwork@qq.com"]];
            // 设置抄送人
    [mailCompose setCcRecipients:@[@"gaogangwork@qq.com"]];
            // 设置密送人
    [mailCompose setBccRecipients:@[@"gaogangwork@qq.com"]];
            // 设置邮件主题
    mailCompose setSubject:@"设置邮件主题"];
            //设置邮件的正文内容
    NSString *emailContent = @"我是邮件内容";
            // 是否为HTML格式
    [mailCompose setMessageBody:emailContent isHTML:NO];
            // 如使用HTML格式,则为以下代码
            // [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World!</p></body></html>" isHTML:YES];
            //添加附件
    UIImage *image = [UIImage imageNamed:@"qq"];
            NSData *imageData = UIImagePNGRepresentation(image);
    [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"qq.png"];
    NSString *file = [[NSBundle mainBundle] pathForResource:@"EmptyPDF" ofType:@"pdf"];
    NSData *pdf = [NSData dataWithContentsOfFile:file];
    [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"EmptyPDF.pdf"];
            // 弹出邮件发送视图
    [_curVC presentViewController:mailCompose animated:YES completion:nil];
}
#pragma mark - MFMailComposeViewControllerDelegate的代理方法:
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail send canceled: 用户取消编辑");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: 用户保存邮件");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent: 用户点击发送");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail send errored: %@ : 用户尝试保存或发送邮件失败", [error localizedDescription]);
            break;
    }
    // 关闭邮件发送视图
    [_curVC dismissViewControllerAnimated:YES completion:nil];
}

# 3. skpsmtpmessage开源

Github地址:skpsmtpmessage (opens new window)

参考

# 四、打开pdf/excel/word/ppt

使用iOS打开pdf/excel/ppt/word,可以使用iOS自带的webview去加载;使用webview首先需要引入#import <WebKit/WebKit.h>

//
//  ViewController.m

#import "ViewController.h"
#import <WebKit/WebKit.h>

@interface ViewController () <MFMessageComposeViewControllerDelegate>
- (IBAction)phone:(id)sender;
- (IBAction)pdf:(id)sender;

@property (nonatomic,strong) WKWebView *webView;

@property (nonatomic,strong) MFMessageComposeViewController *vc;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


//WKWebView加载本地资源
- (IBAction)pdf:(id)sender {
    
    self.webView = [[WKWebView alloc]initWithFrame:self.view.frame];
    
    [self.view addSubview:self.webView];
    
//    [self loadDocument:@"aa.pdf" inView:self.webView];
    
//    [self loadDocument:@"bb.docx" inView:self.webView];
    
    [self loadDocument:@"cc.pptx" inView:self.webView];
    
    //加载网络资源
//    [self loadDocumentFromServer];
}


//WKWebView加载本地资源
-(void)loadDocument:(NSString *)documentName inView:(WKWebView *)webView{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
    
    NSURL *url = [NSURL fileURLWithPath:path];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [webView loadRequest:request];
    
}

// WKWebView加载网络资源
-(void)loadDocumentFromServer{
    
    NSURL *url = [NSURL URLWithString:@"远程的资源url"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];
    
}


@end

【未经作者允许禁止转载】 Last Updated: 10/14/2021, 11:20:21 AM