这篇文章主要介绍了iOS开发中实现邮件和短信发送的简单示例,编程语言依然是传统的Objective-C,需要的朋友可以参考下
发送邮件
1.导入库文件:MessageUI.framework
2.引入头文件
3.实现代理<MFMailComposeViewControllerDelegate> 和 <UINavigationControllerDelegate>
代码示例:
代码如下:
- (void)didClickSendEmailButtonAction{
if ([MFMailComposeViewController canSendMail] == YES) {
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
// 设置代理(与以往代理不同,不是"delegate",千万不能忘记呀,代理有3步)
mailVC.mailComposeDelegate = self;
// 收件人
NSArray *sendToPerson = @[@"humingtao2014@gmail.com"];
[mailVC setToRecipients:sendToPerson];
// 抄送
NSArray *copyToPerson = @[@"humingtao2013@126.com"];
[mailVC setCcRecipients:copyToPerson];
// 密送
NSArray *secretToPerson = @[@"563821250@qq.com"];
[mailVC setBccRecipients:secretToPerson];
// 主题
[mailVC setSubject:@"hello world"];
[self presentViewController:mailVC animated:YES completion:nil];
[mailVC setMessageBody:@"魑魅魍魉,哈哈呵呵嘿嘿霍霍" isHTML:NO];
}else{
NSLog(@"此设备不支持邮件发送");
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"取消发送");
break;
case MFMailComposeResultFailed:
NSLog(@"发送失败");
break;
case MFMailComposeResultSaved:
NSLog(@"保存草稿文件");
break;
case MFMailComposeResultSent:
NSLog(@"发送成功");
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
// 系统发送,模拟器不支持,要用真机测试
- (void)didClickSendSystemEmailButtonAction{
NSURL *url = [NSURL URLWithString:@"humingtao2014@gmail.com"];
if ([[UIApplication sharedApplication] canOpenURL:url] == YES) {
[[UIApplication sharedApplication] openURL:url];
}else{
NSLog(@"此设备不支持");
}
}
发送短信
前面三步引入配置和邮件发送一样
代码如下:
// 调用系统API发送短信
- (void)didClickSendMessageButtonAction{
if ([MFMessageComposeViewController canSendText] == YES) {
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
// 设置代理<MFMessageComposeViewControllerDelegate>
messageVC.messageComposeDelegate = self;
// 发送To Who
messageVC.recipients = @[@"18757289870"];
messageVC.body = @"hello world";
[self presentViewController:messageVC animated:YES completion:nil];
}else{
NSLog(@"此设备不支持");
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
switch (result) {
case MessageComposeResultCancelled:
NSLog(@"取消发送");
break;
case MessageComposeResultFailed:
NSLog(@"发送失败");
break;
case MessageComposeResultSent:
NSLog(@"发送成功");
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
// 调用系统应用程序发送消息
- (void)didClickSendMessage2ButtonAction{
NSURL *url = [NSURL URLWithString:@"sms:18656348970"];
if ([[UIApplication sharedApplication] canOpenURL:url] == YES) {
[[UIApplication sharedApplication] openURL:url];
}else{
NSLog(@"失败");
}
}
沃梦达教程
本文标题为:iOS开发中实现邮件和短信发送的简单示例
基础教程推荐
猜你喜欢
- Android实现短信验证码输入框 2023-04-29
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- iOS开发使用XML解析网络数据 2022-11-12
- Android开发Compose集成高德地图实例 2023-06-15
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- iOS开发 全机型适配解决方法 2023-01-14
- IOS获取系统相册中照片的示例代码 2023-01-03
- Flutter进阶之实现动画效果(三) 2022-10-28
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18