日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS中调用短信和邮箱的方法

發布時間:2023/12/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS中调用短信和邮箱的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//該方法在不退出應用程序的前提下調用短信和郵箱,以下內容請在真機測試

//導入框架MessageUI.framework

#import "ViewController.h"

//首先導入頭文件

#import <MessageUI/MFMailComposeViewController.h>

#import <MessageUI/MFMessageComposeViewController.h>

//代理

@interface ViewController ()<MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>


@end


@implementation ViewController



//郵件按鈕方法實現

- (void)mail:(id)sender {

//判斷設備是否支持應用內發送郵件功能

? ? if ([MFMailComposeViewController canSendMail])? {

?? ? ? ?

//在應用內發送郵件

?? ? ? ?

? ? ? ? //創建郵件controller

? ? ? ? MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];

?? ? ? ?

? ? ? ? //設置郵件代理

? ? ? ? mailPicker.mailComposeDelegate = self;

?? ? ? ?

? ? ? ? //郵件主題

? ? ? ? [mailPicker setSubject:@"Send WebView ScreenShot"];

?? ? ? ?

? ? ? ? //設置發送給誰,參數是NSarray,設置發送給兩個郵箱

? ? ? ? [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];

?? ? ? ?

?? ? ? ?

? ? ? ? //發送圖片附件

? ? ? ? //第一個圖片名字是本地要選擇發送的圖片的名字, 第二個圖片的名字是郵件里發送時顯示的圖片名字

? ? ? ? NSString *pathImage = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg"];

? ? ? ? NSData *dataImage = [NSData dataWithContentsOfFile:pathImage];

? ? ? ? [mailPicker addAttachmentData:dataImage mimeType:@"image/jpg" fileName:@"1.jpg"];

?? ? ? ?

? ? ? ? //發送txt文本附件

? ? ? ? NSString *pathText = [[NSBundle mainBundle] pathForResource:@"tv" ofType:@"txt"];

? ? ? ? NSData *dataText = [NSData dataWithContentsOfFile:pathText];

? ? ? ? [mailPicker addAttachmentData:dataText mimeType:@"text/txt" fileName:@"aa.txt"];

?? ? ? ?

?? ? ? ?

? ? ? ? //發送doc文本附件

? ? ? ? NSString *pathDoc = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];

? ? ? ? NSData *dataDoc = [NSData dataWithContentsOfFile:pathDoc];

? ? ? ? [mailPicker addAttachmentData:dataDoc mimeType:@"text/doc" fileName:@"MyText.doc"];

?? ? ? ?

?? ? ? ?

? ? ? ? //發送pdf文檔附件

?? ? ? ?

? ? ? ? NSString *pathPdf = [[NSBundle mainBundle] pathForResource:@"CodeSigningGuide"ofType:@"pdf"];

? ? ? ? NSData *dataPdf = [NSData dataWithContentsOfFile:pathPdf];

? ? ? ? [mailPicker addAttachmentData:dataPdf mimeType:@"file/pdf"fileName:@"rainy.pdf"];

?? ? ? ?

? ? ? ? //把當前controller變為郵件controller

? ? ? ? [self presentModalViewController:mailPicker animated:YES];

?? ? ? ?

?? ? ? ?

? ? }else{

? ? ? ? //如果該設備不支持在不退出程序的前提下調用郵件,則會推出應用程序并調用系統郵件,mailto://為固定寫法后面加郵箱地址

? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzlzh.com"]];

? ? }

?? ?

}


//實現 MFMailComposeViewControllerDelegate

//發送結果

- (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 = @"點擊發送,將郵件放到隊列中,還沒發送";

? ? ? ? ? ? break;

? ? ? ? case MFMailComposeResultFailed:

? ? ? ? ? ? mes = @"試圖保存或者發送郵件失敗";

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? break;

? ? }

?? ?

? ? UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];

? ? [alter show];

?? ?

?? ?

? ? [self dismissModalViewControllerAnimated:YES];

}




//短信按鈕方法實現

- (IBAction)message:(id)sender {


//判斷設備是否支持應用內發送短信功能


? ? if ([MFMessageComposeViewController canSendText]) {

?? ? ? ?

//在應用內發送短信

? ? ? ? {

? ? ? ? ? ? //初始化

? ? ? ? ? ? MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

? ? ? ? ? ? //代理

? ? ? ? ? ? picker.messageComposeDelegate = self;

? ? ? ? ? ? picker.navigationBar.tintColor = [UIColor blackColor];

? ? ? ? ? ? //短信內容

? ? ? ? ? ? picker.body = @"1111111111111111";

? ? ? ? ? ? //設置發送給誰

? ? ? ? ? ? picker.recipients = [NSArray arrayWithObject:@"13300000000"];

? ? ? ? ? ? //推到發送試圖控制器

? ? ? ? ? ? [self presentModalViewController:picker animated:YES];

?? ? ? ? ? ?

? ? ? ? }

?? ? ? ?

? ? }

? ? else {

? ? ? ? //如果該設備不支持在不退出程序的前提下調用短信,則會推出應用程序并調用系統短信,mailto://為固定寫法后面加手機號碼

? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];

?? ? ? ?

? ? }

?? ?

}




//實現 MFMessageComposeViewControllerDelegate

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

?? ?

? ? NSString *mes = nil;

?? ?

? ? switch (result) {

?? ? ? ? ? ?

? ? ? ? case MessageComposeResultCancelled:

? ? ? ? ? ? mes = @"取消編輯短信";

? ? ? ? ? ? break;

?? ? ? ? ? ?

? ? ? ? case MessageComposeResultSent:

? ? ? ? ? ? mes = @"點擊發送,將短信放到隊列中,還沒發送";

? ? ? ? ? ? break;

?? ? ? ? ? ?

? ? ? ? case MessageComposeResultFailed:

? ? ? ? ? ? mes = @"發送短信失敗";

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? break;

? ? }

?? ?

? ? UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];

? ? [alter show];

?? ?

? ? [self dismissModalViewControllerAnimated:YES];

?? ?

}




總結

以上是生活随笔為你收集整理的iOS中调用短信和邮箱的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。