iOS開發(fā)中實現(xiàn)郵件和短信發(fā)送的簡單示例
更新時間:2015年09月09日 09:25:03 作者:TommyYaphetS
這篇文章主要介紹了iOS開發(fā)中實現(xiàn)郵件和短信發(fā)送的簡單示例,編程語言依然是傳統(tǒng)的Objective-C,需要的朋友可以參考下
發(fā)送郵件
1.導入庫文件:MessageUI.framework
2.引入頭文件
3.實現(xiàn)代理<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(@"此設備不支持郵件發(fā)送");
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"取消發(fā)送");
break;
case MFMailComposeResultFailed:
NSLog(@"發(fā)送失敗");
break;
case MFMailComposeResultSaved:
NSLog(@"保存草稿文件");
break;
case MFMailComposeResultSent:
NSLog(@"發(fā)送成功");
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
// 系統(tǒng)發(fā)送,模擬器不支持,要用真機測試
- (void)didClickSendSystemEmailButtonAction{
NSURL *url = [NSURL URLWithString:@"humingtao2014@gmail.com"];
if ([[UIApplication sharedApplication] canOpenURL:url] == YES) {
[[UIApplication sharedApplication] openURL:url];
}else{
NSLog(@"此設備不支持");
}
}
發(fā)送短信
前面三步引入配置和郵件發(fā)送一樣
復制代碼 代碼如下:
// 調用系統(tǒng)API發(fā)送短信
- (void)didClickSendMessageButtonAction{
if ([MFMessageComposeViewController canSendText] == YES) {
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
// 設置代理<MFMessageComposeViewControllerDelegate>
messageVC.messageComposeDelegate = self;
// 發(fā)送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(@"取消發(fā)送");
break;
case MessageComposeResultFailed:
NSLog(@"發(fā)送失敗");
break;
case MessageComposeResultSent:
NSLog(@"發(fā)送成功");
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
// 調用系統(tǒng)應用程序發(fā)送消息
- (void)didClickSendMessage2ButtonAction{
NSURL *url = [NSURL URLWithString:@"sms:18656348970"];
if ([[UIApplication sharedApplication] canOpenURL:url] == YES) {
[[UIApplication sharedApplication] openURL:url];
}else{
NSLog(@"失敗");
}
}
相關文章
iOS應用設計模式開發(fā)中職責鏈(責任鏈)模式的實現(xiàn)解析
這篇文章主要介紹了iOS應用設計模式開發(fā)中職責鏈模式的相關實現(xiàn)解析,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03ios基于MJRefresh實現(xiàn)上拉刷新和下拉加載動畫效果
本篇文章主要介紹了ios基于MJRefresh實現(xiàn)上拉刷新和下拉加載動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08