邮件、短信

邮件

//
//  ViewController.m
//

#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];

    // 类方法调用2种方式
    // [MFMailComposeViewController canSendMail]
    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

短信

//
//  ViewController.m
//

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值