邮件
#import "ViewController.h"
#import <MessageUI/MessageUI.h> // 库
@interface ViewController ()<MFMailComposeViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_SendMailAction addTarget:self action:@selector(SendMail:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)SendMail:(UIButton*)button{
MFMailComposeViewController *mailvc = [[MFMailComposeViewController alloc] init];
if ([[mailvc class] canSendMail]) {
mailvc.mailComposeDelegate = self;
[mailvc setSubject:@"主题"];
[mailvc setMessageBody:@"内容" isHTML:false];
NSArray * array = @[@"xxxxxxxxx@qq.com",@"xxxxxx@gmail.com"];
[mailvc setToRecipients:array];
[self presentViewController:mailvc animated:true completion:nil];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultSent:
NSLog(@"发送成功");
break;
case MFMailComposeResultFailed:
NSLog(@"发送失败");
break;
case MFMailComposeResultCancelled:
NSLog(@"发送取消");
break;
case MFMailComposeResultSaved:
NSLog(@"save");
break;
default:
break;
}
}
@end
短信
#import "ViewController.h"
#import <MessageUI/MessageUI.h> // 库
@interface ViewController ()<MFMessageComposeViewControllerDelegate>//遵守代理
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_SendMessageButton addTarget:self action:@selector(sendMesssage:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)sendMesssage:(UIButton*)button{
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController * messageVc = [[MFMessageComposeViewController alloc] init];
messageVc.messageComposeDelegate = self;
[messageVc setSubject:@"主题"];
[messageVc setBody:@"内容"];
messageVc.recipients = @[@"188******60"];
[self presentViewController:messageVc animated:true completion:nil];
}
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
switch (result) {
case MessageComposeResultSent:
NSLog(@"信息发送成功");
break;
case MessageComposeResultFailed:
NSLog(@"信息发送失败");
break;
case MessageComposeResultCancelled:
NSLog(@"信息发送取消");
break;
default:
break;
}
}
@end