导入MessageUI.framework
.h文件中#import <MessageUI/MessageUI.h>
#import<MessageUI/MFMailComposeViewController.h>
实现 MFMailComposeViewControllerDelegate,
MFMessageComposeViewControllerDelegate
.m 文件
//短信
-(void)shareFriend{
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
else {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持短信功能" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
[alert show];
}
}
else {
}
}
-(void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate =self;
NSString *smsBody =[NSString stringWithFormat:@"我分享了文件给您,地址是www.imyeliao.com"] ;
picker.body=smsBody;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
// Notifies users about errors associated with the interface
switch (result) {
case MessageComposeResultCancelled:
if (DEBUG) NSLog(@"Result: canceled");
break;
case MessageComposeResultSent:
if (DEBUG) NSLog(@"Result: Sent");
break;
case MessageComposeResultFailed:
if (DEBUG) NSLog(@"Result: Failed");
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
//邮件分享
- (void)shareFriend{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
if ([mailClass canSendMail]) {
[self displayMailComposerSheet];
} else {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
[alert show];
}
} else {
}
}
-(void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate =self;
[picker setSubject:@"文件分享"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients: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:myData mimeType:@"file/pdf"fileName:@"rainy.pdf"];
*/
// Fill out the email body text
NSString *emailBody =[NSString stringWithFormat:@"我分享了文件给您,地址是www.imyeliao.com"] ;
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
}
- (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;
}
[self dismissViewControllerAnimated:YES completion:nil];
}