iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
2016-04-06 15:06
801 查看
导入MessageUI.framework
.h文件中#import<MessageUI/MessageUI.h>
#import<MessageUI/MFMailComposeViewController.h>
实现 MFMailComposeViewControllerDelegate,
MFMessageComposeViewControllerDelegate
.m
文件
//邮件
-(void)showMailPicker {
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass !=nil) {
if ([mailClass canSendMail]) {
[selfdisplayMailComposerSheet];
}else{
UIAlertView *alert=[[UIAlertView
alloc] initWithTitle:@""message:@"设备不支持邮件功能"
delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert
show];
[alert
release];
}
}else{
}
}
-(void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc]init];
picker.mailComposeDelegate =self;
[pickersetSubject:@"文件分享"];
// Set up recipients
NSArray *toRecipients = [NSArrayarrayWithObject:@"first@qq.com"];
NSArray *ccRecipients = [NSArrayarrayWithObjects:@"second@qq.com",@"third@qq.com",nil];
NSArray *bccRecipients = [NSArrayarrayWithObject:@"fourth@qq.com"];
[pickersetToRecipients:toRecipients];
[pickersetCcRecipients:ccRecipients];
[pickersetBccRecipients:bccRecipients];
//发送图片附件
//NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy.jpg"];
//发送txt文本附件
//NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"];
//发送doc文本附件
//NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"];
//发送pdf文档附件
/*
NSString *path = [[NSBundlemainBundle]pathForResource:@"CodeSigningGuide"ofType:@"pdf"];
NSData *myData = [NSDatadataWithContentsOfFile:path];
[pickeraddAttachmentData:myDatamimeType:@"file/pdf"fileName:@"rainy.pdf"];
*/
// Fill out the email body text
NSString *emailBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address]
;
[pickersetMessageBody:emailBodyisHTML:NO];
[selfpresentModalViewController:pickeranimated:YES];
[pickerrelease];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
// Notifies users about errors associated with the interface
switch (result)
{
caseMFMailComposeResultCancelled:
NSLog(@"Result: Mail sending canceled");
break;
caseMFMailComposeResultSaved:
NSLog(@"Result: Mail saved");
break;
caseMFMailComposeResultSent:
NSLog(@"Result: Mail sent");
break;
caseMFMailComposeResultFailed:
NSLog(@"Result: Mail sending failed");
break;
default:
NSLog(@"Result: Mail not sent");
break;
}
[selfdismissModalViewControllerAnimated:YES];
}
//短信
-(void)showSMSPicker{
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[selfdisplaySMSComposerSheet];
}
else {
UIAlertView *alert=[[UIAlertView
alloc] initWithTitle:@""message:@"设备不支持短信功能"
delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert
show];
[alert
release];
}
}
else {
}
}
-(void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc]init];
picker.messageComposeDelegate =self;
NSString *smsBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address]
;
picker.body=smsBody;
[selfpresentModalViewController:pickeranimated:YES];
[pickerrelease];
}
.h文件中#import<MessageUI/MessageUI.h>
#import<MessageUI/MFMailComposeViewController.h>
实现 MFMailComposeViewControllerDelegate,
MFMessageComposeViewControllerDelegate
.m
文件
//邮件
-(void)showMailPicker {
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass !=nil) {
if ([mailClass canSendMail]) {
[selfdisplayMailComposerSheet];
}else{
UIAlertView *alert=[[UIAlertView
alloc] initWithTitle:@""message:@"设备不支持邮件功能"
delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert
show];
[alert
release];
}
}else{
}
}
-(void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc]init];
picker.mailComposeDelegate =self;
[pickersetSubject:@"文件分享"];
// Set up recipients
NSArray *toRecipients = [NSArrayarrayWithObject:@"first@qq.com"];
NSArray *ccRecipients = [NSArrayarrayWithObjects:@"second@qq.com",@"third@qq.com",nil];
NSArray *bccRecipients = [NSArrayarrayWithObject:@"fourth@qq.com"];
[pickersetToRecipients:toRecipients];
[pickersetCcRecipients:ccRecipients];
[pickersetBccRecipients:bccRecipients];
//发送图片附件
//NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy.jpg"];
//发送txt文本附件
//NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"];
//发送doc文本附件
//NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"];
//发送pdf文档附件
/*
NSString *path = [[NSBundlemainBundle]pathForResource:@"CodeSigningGuide"ofType:@"pdf"];
NSData *myData = [NSDatadataWithContentsOfFile:path];
[pickeraddAttachmentData:myDatamimeType:@"file/pdf"fileName:@"rainy.pdf"];
*/
// Fill out the email body text
NSString *emailBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address]
;
[pickersetMessageBody:emailBodyisHTML:NO];
[selfpresentModalViewController:pickeranimated:YES];
[pickerrelease];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
// Notifies users about errors associated with the interface
switch (result)
{
caseMFMailComposeResultCancelled:
NSLog(@"Result: Mail sending canceled");
break;
caseMFMailComposeResultSaved:
NSLog(@"Result: Mail saved");
break;
caseMFMailComposeResultSent:
NSLog(@"Result: Mail sent");
break;
caseMFMailComposeResultFailed:
NSLog(@"Result: Mail sending failed");
break;
default:
NSLog(@"Result: Mail not sent");
break;
}
[selfdismissModalViewControllerAnimated:YES];
}
//短信
-(void)showSMSPicker{
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[selfdisplaySMSComposerSheet];
}
else {
UIAlertView *alert=[[UIAlertView
alloc] initWithTitle:@""message:@"设备不支持短信功能"
delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert
show];
[alert
release];
}
}
else {
}
}
-(void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc]init];
picker.messageComposeDelegate =self;
NSString *smsBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address]
;
picker.body=smsBody;
[selfpresentModalViewController:pickeranimated:YES];
[pickerrelease];
}
相关文章推荐
- 快速理解GCD
- iOS-iphone自定义状态栏
- ios-获取系统相簿里边的所有照片
- iOS-自定义修改拍照界面retake和use按钮
- iOS 8 AutoLayout与Size Class
- iOS开发入门的一些困惑
- 【ios开发学习】Cocoa 新的依赖管理工具:Carthage
- iOS图片旋转问题
- iOS 画平滑曲线的方法及取音频数据的方法
- iOS 数据保存几种方式总结
- iOS开发使用-半透明-模糊效果-方法整理
- 神奇的ios9bug
- 关于这几天使用IOS的ASYNCSOCKET完成无限后台的过程
- iOS项目的完整重命名方法
- ios Block详细用法
- RealReachability的使用心得
- iOS 音频播放,录音,视频播放,拍照,视频录制
- Responder Chain(ios事件传递)
- iOS runTime 机制详解
- iOS日期相关