iOS开发之打电话,发短信,发送邮件
【摘要】
iOS开发中,拨打电话的实现主要有三种方式:
直接拨号:拨打完电话之后回不到原来的应用,会停留在通讯录里面,而且是直接拨打,不弹出提示
- (void)makePhoneCall {
NSMu...
iOS开发中,拨打电话的实现主要有三种方式:
- 直接拨号:拨打完电话之后回不到原来的应用,会停留在通讯录里面,而且是直接拨打,不弹出提示
- (void)makePhoneCall {
NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"tel:%@",@“10086"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
}
- 1
- 2
- 3
- 4
- 跳出应用打完电话之后回到应用
//telprompt协议属于苹果的私有协议,一旦程序中使用了此协议,程序无法上架。针对越狱的机器开发的系统,可以使用此协议。
- (void)makePhoneCall {
NSMutableString *string = [[NSMutableString alloc] initWithFormat:@“telprompt://%@",@“10086"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
}
- 1
- 2
- 3
- 4
- 5
- 借助UIWebView打电话
- (void)makePhoneCall {
NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"tel:%@",@“10086"];
UIWebView *callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
[self.view addSubview:callWebview];
}
- 1
- 2
- 3
- 4
- 5
- 6
iOS调用系统的发短信功能主要有两种:
- 程序外调用系统发短信(不能指定短信内容,且不能自动回到原应用)
NSURL *url = [NSURL URLWithString:@"sms://10010"];
[[UIApplication sharedApplication] openURL:url];
- 1
- 2
程序内调用系统发短信(发完短信之后可以回到App)
1.导入MessageUI.framework,并实现代理方法MFMessageComposeViewControllerDelegate
#import<MessageUI/MessageUI.h>
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:nil];
switch(result){
caseMessageComposeResultSent:
//信息传送成功
break;
caseMessageComposeResultFailed:
//信息传送失败
break;
caseMessageComposeResultCancelled:
//信息被用户取消传送
break;
default:
break;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
2.发送短信
- (void)showMessageView:(NSArray*)phones title:(NSString*)title body:(NSString*)body {
// 判断用户设备能否发送短信
if([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
//收件人:phones是发短信的手机号码的数组,数组中是一个即单发,多个即群发
controller.recipients = phones;
//短信内容
controller.body = body;
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
//修改短信界面标题
[[[[controllerviewControllers] lastObject] navigationItem] setTitle:title];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
message:@"该设备不支持短信功能"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil,nil];
[alertshow];
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
3.调用发短信的方法
[self showMessageView:[NSArrayarrayWithObjects:@"10086",@"10010",nil] title:@"恭喜" body:@"你们中五百万大奖啦!!"];
- 1
iOS上可以使用三种方法实现邮件的发送:
- 使用内置的MFMailComposeViewController发送邮件
1、导入MessageUI,实现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;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
2、检测设备是否支持邮件发送功能
//检测设备是否支持邮件发送功能
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
if ([mailClass canSendMail]) {
[self displayComposerSheet];//调用发送邮件的方法
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
3.邮件发送方法:
- (void)sendE-mail {
//创建视图控制器
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
//设置邮件主题
[mc setSubject:@"Hello, World!"];
//设置收件人,收件人有三种:主收件人,cc,bcc
[mc setToRecipients:[NSArray arrayWithObjects:@"xxx@126.com", nil];
[mc setCcRecipients:[NSArray arrayWithObject:@"xxx@163.com"]];
[mc setBccRecipients:[NSArray arrayWithObject:@"xxx@gmail.com"]];
//设置邮件主体,有两种格式:纯文本,html格式
[mc setMessageBody:@"Watson!!!\n\nCome here, I need you!" isHTML:NO];
[mc setMessageBody:@"<HTML><B>Hello, Joe!</B><BR/>What do you know?</HTML>" isHTML:YES];
//添加附件:需要三个参数,一个是NSData类型的附件,一个是mime type,一个附件的名称
NSString *path = [[NSBundle mainBundle] pathForResource:@"blood_orange"
ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:path];
[mc addAttachmentData:data mimeType:@"image/png" fileName:@"blood_orange"];
//视图呈现
[self presentModalViewController:mc animated:YES];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 通过第三方类库SKPSMTPMessage发送邮件
1、下载三方库,导入类#import “SKPSMTPMessage.h”、#import “NSData+Base64Additions.h”,实现SKPSMTPMessage代理
//成功
- (void)messageSent:(SKPSMTPMessage *)message {
NSLog(@"%@", message);
}
//失败
- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error {
NSLog(@"message - %@\nerror - %@", message, error);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2、邮件发送方法
- (void)sendE-mail {
//设置基本参数
SKPSMTPMessage *mail = [[SKPSMTPMessage alloc] init];
[mail setSubject:@"主题"]; // 设置邮件主题
[mail setToEmail:@"xxx@qq.com"]; // 目标邮箱
[mail setFromEmail:@"xxx@qq.com"]; // 发送者邮箱
[mail setRelayHost:@"smtp.qq.com"]; // 发送邮件代理服务器
[mail setRequiresAuth:YES];
[mail setLogin:@"xxx@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"};
//添加附件
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.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
[mail send];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 使用openURL发送邮件(用户体验较差,程序会进入后台,跳转至邮件发送界面)
- (void)sendE-mail {
//创建可变的地址字符串对象
NSMutableString *mailUrl = [[NSMutableString alloc] init];
//添加收件人
NSArray *ccRecipients = @[@"1229436624@qq.com"];
[mailUrl appendFormat:@"?cc=%@", ccRecipients[0]];
//添加密送人
NSArray *bccRecipients = @[@"shana_happy@126.com"];
[mailUrl appendFormat:@"&bcc=%@", bccRecipients[0]];
//添加邮件主题和邮件内容
[mailUrl appendString:@"&subject=my email"];
[mailUrl appendString:@"&body=<b>Hello</b> World!"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailUrl]];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
文章来源: blog.csdn.net,作者:Serendipity·y,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Forever_wj/article/details/51024426
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)