iOS中调用短信和邮箱的方法
//該方法在不退出應(yīng)用程序的前提下調(diào)用短信和郵箱,以下內(nèi)容請(qǐng)?jiān)谡鏅C(jī)測(cè)試
//導(dǎo)入框架MessageUI.framework
#import "ViewController.h"
//首先導(dǎo)入頭文件
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>
//代理
@interface ViewController ()<MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
@end
@implementation ViewController
//郵件按鈕方法實(shí)現(xiàn)
- (void)mail:(id)sender {
//判斷設(shè)備是否支持應(yīng)用內(nèi)發(fā)送郵件功能
? ? if ([MFMailComposeViewController canSendMail])? {
?? ? ? ?
//在應(yīng)用內(nèi)發(fā)送郵件
?? ? ? ?
? ? ? ? //創(chuàng)建郵件controller
? ? ? ? MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
?? ? ? ?
? ? ? ? //設(shè)置郵件代理
? ? ? ? mailPicker.mailComposeDelegate = self;
?? ? ? ?
? ? ? ? //郵件主題
? ? ? ? [mailPicker setSubject:@"Send WebView ScreenShot"];
?? ? ? ?
? ? ? ? //設(shè)置發(fā)送給誰,參數(shù)是NSarray,設(shè)置發(fā)送給兩個(gè)郵箱
? ? ? ? [mailPicker setToRecipients:[NSArray arrayWithObjects:@"aaaaa@163.com", @"aaaaaa@qq.com", nil]];
?? ? ? ?
?? ? ? ?
? ? ? ? //可以添加抄送
? ? ? ? [mailPicker setCcRecipients:[NSArray arrayWithObject:@"aaaaa@qq.com"]];
?? ? ? ?
?? ? ? ?
? ? ? ? //可以添加暗抄送
? ? ? ? [mailPicker setBccRecipients:[NSArray arrayWithObject:@"aaaaaa@qq.com"]];
?? ? ? ?
?? ? ? ?
? ? ? ? //郵件正文
? ? ? ? [mailPicker setMessageBody:@"WebShotScreen n in Attachment!" isHTML:NO];
?? ? ? ?
?? ? ? ?
? ? ? ? //發(fā)送圖片附件
? ? ? ? //第一個(gè)圖片名字是本地要選擇發(fā)送的圖片的名字, 第二個(gè)圖片的名字是郵件里發(fā)送時(shí)顯示的圖片名字
? ? ? ? NSString *pathImage = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg"];
? ? ? ? NSData *dataImage = [NSData dataWithContentsOfFile:pathImage];
? ? ? ? [mailPicker addAttachmentData:dataImage mimeType:@"image/jpg" fileName:@"1.jpg"];
?? ? ? ?
? ? ? ? //發(fā)送txt文本附件
? ? ? ? NSString *pathText = [[NSBundle mainBundle] pathForResource:@"tv" ofType:@"txt"];
? ? ? ? NSData *dataText = [NSData dataWithContentsOfFile:pathText];
? ? ? ? [mailPicker addAttachmentData:dataText mimeType:@"text/txt" fileName:@"aa.txt"];
?? ? ? ?
?? ? ? ?
? ? ? ? //發(fā)送doc文本附件
? ? ? ? NSString *pathDoc = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];
? ? ? ? NSData *dataDoc = [NSData dataWithContentsOfFile:pathDoc];
? ? ? ? [mailPicker addAttachmentData:dataDoc mimeType:@"text/doc" fileName:@"MyText.doc"];
?? ? ? ?
?? ? ? ?
? ? ? ? //發(fā)送pdf文檔附件
?? ? ? ?
? ? ? ? NSString *pathPdf = [[NSBundle mainBundle] pathForResource:@"CodeSigningGuide"ofType:@"pdf"];
? ? ? ? NSData *dataPdf = [NSData dataWithContentsOfFile:pathPdf];
? ? ? ? [mailPicker addAttachmentData:dataPdf mimeType:@"file/pdf"fileName:@"rainy.pdf"];
?? ? ? ?
? ? ? ? //把當(dāng)前controller變?yōu)猷]件controller
? ? ? ? [self presentModalViewController:mailPicker animated:YES];
?? ? ? ?
?? ? ? ?
? ? }else{
? ? ? ? //如果該設(shè)備不支持在不退出程序的前提下調(diào)用郵件,則會(huì)推出應(yīng)用程序并調(diào)用系統(tǒng)郵件,mailto://為固定寫法后面加郵箱地址
? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzlzh.com"]];
? ? }
?? ?
}
//實(shí)現(xiàn) MFMailComposeViewControllerDelegate
//發(fā)送結(jié)果
- (void)mailComposeController:(MFMailComposeViewController*)controller
? ? ? ? ? didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
?? ?
? ? NSString *mes = nil;
?? ?
? ? switch (result)
? ? {
? ? ? ? case MFMailComposeResultCancelled:
? ? ? ? ? ? mes = @"取消編輯郵件";
? ? ? ? ? ? break;
? ? ? ? case MFMailComposeResultSaved:
? ? ? ? ? ? mes = @"成功保存郵件";
? ? ? ? ? ? break;
? ? ? ? case MFMailComposeResultSent:
? ? ? ? ? ? mes = @"點(diǎn)擊發(fā)送,將郵件放到隊(duì)列中,還沒發(fā)送";
? ? ? ? ? ? break;
? ? ? ? case MFMailComposeResultFailed:
? ? ? ? ? ? mes = @"試圖保存或者發(fā)送郵件失敗";
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? break;
? ? }
?? ?
? ? UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
? ? [alter show];
?? ?
?? ?
? ? [self dismissModalViewControllerAnimated:YES];
}
//短信按鈕方法實(shí)現(xiàn)
- (IBAction)message:(id)sender {
//判斷設(shè)備是否支持應(yīng)用內(nèi)發(fā)送短信功能
? ? if ([MFMessageComposeViewController canSendText]) {
?? ? ? ?
//在應(yīng)用內(nèi)發(fā)送短信
? ? ? ? {
? ? ? ? ? ? //初始化
? ? ? ? ? ? MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
? ? ? ? ? ? //代理
? ? ? ? ? ? picker.messageComposeDelegate = self;
? ? ? ? ? ? picker.navigationBar.tintColor = [UIColor blackColor];
? ? ? ? ? ? //短信內(nèi)容
? ? ? ? ? ? picker.body = @"1111111111111111";
? ? ? ? ? ? //設(shè)置發(fā)送給誰
? ? ? ? ? ? picker.recipients = [NSArray arrayWithObject:@"13300000000"];
? ? ? ? ? ? //推到發(fā)送試圖控制器
? ? ? ? ? ? [self presentModalViewController:picker animated:YES];
?? ? ? ? ? ?
? ? ? ? }
?? ? ? ?
? ? }
? ? else {
? ? ? ? //如果該設(shè)備不支持在不退出程序的前提下調(diào)用短信,則會(huì)推出應(yīng)用程序并調(diào)用系統(tǒng)短信,mailto://為固定寫法后面加手機(jī)號(hào)碼
? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];
?? ? ? ?
? ? }
?? ?
}
//實(shí)現(xiàn) MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
?? ?
? ? NSString *mes = nil;
?? ?
? ? switch (result) {
?? ? ? ? ? ?
? ? ? ? case MessageComposeResultCancelled:
? ? ? ? ? ? mes = @"取消編輯短信";
? ? ? ? ? ? break;
?? ? ? ? ? ?
? ? ? ? case MessageComposeResultSent:
? ? ? ? ? ? mes = @"點(diǎn)擊發(fā)送,將短信放到隊(duì)列中,還沒發(fā)送";
? ? ? ? ? ? break;
?? ? ? ? ? ?
? ? ? ? case MessageComposeResultFailed:
? ? ? ? ? ? mes = @"發(fā)送短信失敗";
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? break;
? ? }
?? ?
? ? UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
? ? [alter show];
?? ?
? ? [self dismissModalViewControllerAnimated:YES];
?? ?
}
總結(jié)
以上是生活随笔為你收集整理的iOS中调用短信和邮箱的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: threejs中PC与手机操作的一些总结
- 下一篇: PIC单片机入门_定时器/计数器TMR0