oc中打电话/发短息/发邮件的方法总结

打电话的三种方法:
1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示
NSMutableString  * str=[[ NSMutableString   alloc initWithFormat : @"tel:%@" , @"1333333333"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    

2,这种方法,打完电话后还会回到原来的程序,也会弹出提示,推荐这种
NSMutableString * str=[[NSMutableString allocinitWithFormat:@"tel:%@",@"10000"];
    
UIWebView * cWebview = [[UIWebView allocinit];
    [callWebview 
loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [
self.view addSubview:callWebview];
    


3,这种方法也会回去到原来的程序里(注意这里的telprompt),也会弹出提示,这种方法不合法,上传到AppStore无效
NSMutableString  * str=[[ NSMutableString   alloc initWithFormat : @"telprompt://%@" , @" 10000 " ];
    [[UIApplication sharedApplicationopenURL:[NSURL URLWithString:str]];
=======================================
发短信:

iOS调用系统的发短信功能可以分为两种:1,程序外调用系统发短信。2,程序内调用系统发短信。第二种的好处是用户发短信之后还可以回到app。这对app来说非常重要。

程序外调用系统发短信

这个方法其实很简单,直接调用openURL即可:

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@sms://1333338888]];

程序内调用系统发短信(发完短信回到应用程序)

1)导入MessageUI.framework,并引入头文件:
2)实现代理方法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;
    }
}
3)发送短信
  -(void)showMessageView:(NSArray *)phones title:(NSString *)title 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];
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@提示信息
                                                        message:@该设备不支持短信功能
                                                       delegate:nil
                                              cancelButtonTitle:@确定
                                              otherButtonTitles:nil, nil];
        [alert show];
    }
}

参数phones:发短信的手机号码的数组,数组中是一个即单发,多个即群发。

4)调用发短信的方法
    [self showMessageView:[NSArray arrayWithObjects:@13888888888,@13999999999, nil] title:@test body:@你是土豪么,么么哒];

========================================
发邮件:
方法一: 使用openURL发送邮件;用户体验较差,程序会进入后台,跳转至邮件发送界面。(原生)

创建可变的地址字符串对象:

NSMutableString *mailUrl = [[NSMutableString alloc] init];

添加收件人:

NSArray *toRecipients = @[@"写你们自己的邮箱测试@qq.com"]; // 注意:如有多个收件人,可以使用componentsJoinedByString方法连接,连接符为@","
[mailUrl appendFormat:@"mailto:%@", toRecipients[0]];

添加抄送人:

NSArray *ccRecipients = @[@"1229436624@qq.com"]; [mailUrl appendFormat:@"?cc=%@", ccRecipients[0]];
View Code

添加密送人:

NSArray *bccRecipients = @[@"shana_happy@126.com"]; [mailUrl appendFormat:@"&bcc=%@", bccRecipients[0]];
View Code

添加邮件主题和邮件内容:

[mailUrl appendString:@"&subject=my email"];
[mailUrl appendString:@"&body=<b>Hello</b> World!"];
[[UIApplication  sharedApplication ]  openURL : [NSURL  URLWithString :  mailUrl ]]; 


=====
方法二:不会进入后台,使用模态弹出邮件发送视图。

二、使用MFMailComposeViewController发送邮件(原生)

使用前注意:

1)项目需要导入框架:MessageUI.framework

2)使用的Controlelr里导入头文件:#import <MessageUI/MessageUI.h>

获取用户是否设置了邮件账户:

if ([MFMailComposeViewController canSendMail]) { // 用户已设置邮件账户
    [self sendEmailAction]; // 调用发送邮件的代码
}

sendEmailAction方法代码:

- (void)sendEmailAction
{
 // 邮件服务器
  MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
 // 设置邮件代理
 [mailCompose setMailComposeDelegate:self];
 // 设置邮件主题
  [mailCompose setSubject:@"我是邮件主题"];
 // 设置收件人
  [mailCompose setToRecipients:@[@"[email protected]"]];
 // 设置抄送人
  [mailCompose setCcRecipients:@[@"[email protected]"]];
 // 设置密抄送
  [mailCompose setBccRecipients:@[@"[email protected]"]];
 /**   * 设置邮件的正文内容   */
 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:@"image"];
 NSData *imageData = UIImagePNGRepresentation(image);
 [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"custom.png"];
 NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
 NSData *pdf = [NSData dataWithContentsOfFile:file];
 [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"dddddd"];
 // 弹出邮件发送视图
 [self presentViewController:mailCompose animated:YES completion:nil];
}

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;
 }
 // 关闭邮件发送视图
 [self dismissViewControllerAnimated:YES completion:nil];
}

====
放法三:

三、 使用第三方库SKPSMTPMessage发送邮件

使用前注意:

1)下载第三方库

2)导入类#import "SKPSMTPMessage.h"、#import "NSData+Base64Additions.h"

设置基本参数:

SKPSMTPMessage *mail = [[SKPSMTPMessage alloc] init];
[mail setSubject:@"我是主题"];  // 设置邮件主题
[mail setToEmail:@"填你们自己的@qq.com"]; // 目标邮箱
[mail setFromEmail:@"填你们自己的@qq.com"]; // 发送者邮箱
[mail setRelayHost:@"smtp.qq.com"]; // 发送邮件代理服务器
[mail setRequiresAuth:YES];
[mail setLogin:@"填你们自己的@qq.com"]; // 发送者邮箱账号
[mail setPass:@"填你们自己的"]; // 发送者邮箱密码
[mail setWantsSecure:YES];  // 需要加密
[mail setDelegate:self];

设置邮件正文内容:

NSString *content = [NSString stringWithCString:"测试内容" encoding:NSUTF8StringEncoding];
NSDictionary *plainPart = @{kSKPSMTPPartContentTypeKey : @"text/plain", kSKPSMTPPartMessageKey : content, kSKPSMTPPartContentTransferEncodingKey : @"8bit"};

添加附件(以下代码可在SKPSMTPMessage库的DMEO里找到):

NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];
NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];
NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,
                         @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

执行发送邮件代码:

[mail setParts:@[plainPart, vcfPart]]; // 邮件首部字段、邮件内容格式和传输编码
[mail send];

SKPSMTPMessage代理,可以获知成功/失败进行后续步骤处理:

- (void)messageSent:(SKPSMTPMessage *)message
{
 NSLog(@"%@", message); } - (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error {  NSLog(@"message - %@\nerror - %@", message, error);
}




你可能感兴趣的:(oc中打电话/发短息/发邮件的方法总结)