ios 系统发邮件、短信的实现
//
// WFSystemLib.h
// news
//
// Created by wolvesqun on 14-1-8.
// Copyright (c) 2014年 wolvesqun. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MessageUI/MFMessageComposeViewController.h>
#import <MessageUI/MFMailComposeViewController.h>
/**
* 系统功能类
*/
@interface WFSystemLib : NSObject<MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>
/**
* @brief 发送短信
*
* @param vc 视图控制器 (必须)
*
* @param bodyOfMsg 短信内容
*
* @param recipients 好友列表
*
* @return
*/
+ (void)sendMsg:(UIViewController *)vc bodyOfMsg:(NSString *)bodyOfMsg recipients:(NSArray *)recipients;
/**
* @brief 发送邮件
*
* @param vc 视图控制器 (必须)
*
* @param subject 主题
*
* @param content 邮件内容
*
* @return
*/
+ (void)sendEmail:(UIViewController *)vc subject:(NSString *)subject content:(NSString *)content;
@end
2. m部分
//
// WFSystemLib.m
// news
//
// Created by wolvesqun on 14-1-8.
// Copyright (c) 2014年 wolvesqun. All rights reserved.
//
#import "WFSystemLib.h"
@interface WFSystemLib ()
@property (strong, nonatomic) UIViewController *vc;
@end
@implementation WFSystemLib
/**
* @brief 发送短信
*
* @param vc 视图控制器
*
* @param bodyOfMsg 短信内容
*
* @param recipients 好友列表
*
* @return
*/
+ (void)sendMsg:(UIViewController *)vc bodyOfMsg:(NSString *)bodyOfMsg recipients:(NSArray *)recipients {
if(vc == nil) {
[self showException];
return;
}
WFSystemLib *systemLib = [WFSystemLib shareInstanced];
systemLib.vc = vc;
[systemLib sendSMS:bodyOfMsg recipientList:recipients];
}
/**
* @brief 发送邮件
*
* @param vc 视图控制器
*
* @param subject 主题
*
* @param content 邮件内容
*
* @return
*/
+ (void)sendEmail:(UIViewController *)vc subject:(NSString *)subject content:(NSString *)content {
if(vc == nil) {
[self showException];
return;
}
WFSystemLib *systemLib = [WFSystemLib shareInstanced];
systemLib.vc = vc;
[systemLib sendEmail:subject content:content];
}
/**
*
*/
+ (void)showException {
NSException *exception = [NSException exceptionWithName:@"SystemLib 位置" reason:@"视图控制器为 nil" userInfo:nil];
@throw exception;
}
/**
* @brief 初始化
*/
static WFSystemLib *systemLib;
+ (id)shareInstanced {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if(systemLib == nil) {
systemLib = [WFSystemLib new];
}
});
return systemLib;
}
#pragma mark - 发送短信
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
// *** 短信视图
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if (IOS7) {
[controller.navigationBar setBarTintColor:[UIColor whiteColor]];
[controller.navigationBar setTintColor:[UIColor whiteColor]];
// [controller.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];
}else{
[controller.navigationBar setTintColor:[UIColor whiteColor]];
}
[controller.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:17.0], NSFontAttributeName, nil]];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self.vc presentViewController:controller animated:YES completion:nil];
}
}
#pragma mark - 发送短信完成后回调
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self.vc dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 发送邮件
-(void)sendEmail:(NSString *)subject content:(NSString *)content{
// *** 邮件视图
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
if (IOS7) {
[controller.navigationBar setBarTintColor:[UIColor whiteColor]];
[controller.navigationBar setTintColor:[UIColor whiteColor]];
// [controller.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];
}else{
[controller.navigationBar setTintColor:[UIColor whiteColor]];
}
[controller.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:17.0], NSFontAttributeName, nil]];
if([MFMailComposeViewController canSendMail])
{
[controller setSubject:subject];
[controller setMessageBody:content isHTML:NO];
controller.mailComposeDelegate = self;
[self.vc presentViewController:controller animated:YES completion:nil];
}
}
#pragma mark - 邮件发送完成处理
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
[self.vc dismissViewControllerAnimated:YES completion:nil];
}
@end