创建MFMessageComposeViewController对象。
设置收件人recipients、
信息正文body,
主题subject
附件attachments(可以通过canSendSubject、canSendAttachments方法判断是否支持)
设置代理messageComposeDelegate(注意这里不是delegate属性,因为delegate属性已经留给UINavigationController,MFMessageComposeViewController没有覆盖此属性而是重新定义了一个代理),实现代理方法获得发送状态。
isSupportedAttachmentUTI 判断是否支持统一标识附件
根据URL路径和添加附件,返回YES表示添加成功
- (BOOL)addAttachmentURL:(NSURL *)attachmentURL withAlternateFilename:(NSString *)alternateFilename;
根据Data数据添加附件
- (BOOL)addAttachmentData:(NSData *)attachmentData typeIdentifier:(NSString *)uti filename:(NSString *)filename;
#import"ViewController.h"
//引入框架
#import
@interfaceViewController()
@property(nonatomic,strong)UIWebView*webView;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton*phoneButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
phoneButton.frame=CGRectMake(100,100,100,100);
[phoneButtonsetTitle:@"打电话"forState:UIControlStateNormal];
[phoneButtonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[phoneButtonaddTarget:selfaction:@selector(phoneButtonAction:)forControlEvents:UIControlEventTouchUpInside];
UIButton*messageButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
messageButton.frame=CGRectMake(100,240,100,100);
[messageButtonsetTitle:@"发信息"forState:UIControlStateNormal];
[messageButtonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[messageButtonaddTarget:selfaction:@selector(messageButtonAction:)forControlEvents:UIControlEventTouchUpInside];
UIButton*emailButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
emailButton.frame=CGRectMake(100,400,100,100);
[emailButtonsetTitle:@"发邮件"forState:UIControlStateNormal];
[emailButtonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[emailButtonaddTarget:selfaction:@selector(emailButtonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:phoneButton];
[self.viewaddSubview:messageButton];
[self.viewaddSubview:emailButton];
}
//电话有三种方法
- (void)phoneButtonAction:(UIButton*)button
{
//法一:没有弹窗提示直接运行不能回到原来的应用程序
// tel://是代码规范
// NSURL *url = [NSURL URLWithString:@"tel://15385548670"];
// [[UIApplication sharedApplication] openURL:url];
//法二:有弹窗提示可以回到原来的应用程序
// telprompt这个是苹果的私有方法应用程序中如果使用了这个方法审核会被驳回
// NSURL *url = [NSURL URLWithString:@"telprompt://15385548670"];
// [[UIApplication sharedApplication] openURL:url];
//法三:借助webView打电话有弹窗会回到原来的应用程序建议使用
//懒加载不要将webView添加到self.view如果添加的话会遮挡原应用程序
if(_webView==nil) {
_webView= [[UIWebViewalloc]init];
}
NSURL*url = [NSURLURLWithString:@"tel://15385548670"];
//创建一个请求
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
//通过请求加载webView
[_webViewloadRequest:request];
}
// message和email需要需要添加一个一个框架
- (void)messageButtonAction:(UIButton*)button
{
if([MFMessageComposeViewControllercanSendText]) {
//初始化一个控制器
MFMessageComposeViewController*messageVC = [[MFMessageComposeViewControlleralloc]init];
//设置短信内容
//设置收件人可设置多个
messageVC.recipients=@[@"15385548670"];
//设置短信内容
messageVC.body=@"";
//设置代理
messageVC.messageComposeDelegate=self;
//跳转
[selfpresentViewController:messageVCanimated:YEScompletion:nil];
}else{
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"提示信息"
message:@"该设备不支持短信功能"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil,nil];
[alertshow];
}
}
- (void)emailButtonAction:(UIButton*)button
{
if([MFMailComposeViewControllercanSendMail]) {
//初始化一个控制器
MFMailComposeViewController*mailVC = [[MFMailComposeViewControlleralloc]init];
//设置邮件主题
[mailVCsetSubject:@"主题"];
//设置收件人
[mailVCsetToRecipients:@[@"[email protected]"]];
//设置正文
[mailVCsetMessageBody:@"正文"isHTML:NO];
//添加附件
UIImage*image = [UIImageimageNamed:@"1.png"];
//把UIImage转化成NSData
NSData*data =UIImagePNGRepresentation(image);
[mailVCaddAttachmentData:datamimeType:@"image/png"fileName:@"2.png"];
//设置代理
mailVC.mailComposeDelegate=self;
//跳转界面
[selfpresentViewController:mailVCanimated:YEScompletion:nil];
}
}
//发送信息的实现代理方法
- (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result {[selfdismissViewControllerAnimated:YEScompletion:nil];}
//发送邮件代理方法的实现
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[selfdismissViewControllerAnimated:YEScompletion:nil];
}