发邮件、发信息、打电话

由于不知道怎么设置简述格式,无法上传代码文件,所以就随便贴上代码

导入MessageUI.frameWork

#import@interface SimpleCallViewController : UIViewController

/**

*  打电话

*

*  @param number                  电话号码

*  @param inViewController    需要打电话的控制器

*/

+(void)call:(NSString *)number inViewController:(UIViewController *)vc failBlock:(void(^)())failBlock;

/**

*  发短信

*

*  @param message                  短信内容

*  @param inViewController    需要发短信的控制器

*/

+(void)sendMessage:(NSString *)message numb:(NSString *)number inViewController:(UIViewController *)vc failBlock:(void(^)())failBlock;

/**

*  发邮件

*

*  @param message                  短信内容

*  @param inViewController    需要发短信的控制器

*/

+(void)sendMailInViewController:(UIViewController *)vc failBlock:(void(^)())failBlock;

@end

#import "SimpleCallViewController.h"#import@interface SimpleCallViewController (){

UIViewController *viewController;

}

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation SimpleCallViewController

- (instancetype)init

{

self = [super init];

if (self) {

_webView=[[UIWebView alloc] init];

}

return self;

}

-(void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

[self removeFromParentViewController];

}


+(void)call:(NSString *)number inViewController:(UIViewController *)vc failBlock:(void(^)())failBlock

{

//拨打电话

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",number]];

//  方法一、使用openURL这个API打电话结束后,返回的是系统的拨打电话界面

BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];

if(!canOpen){//不能打开

if(failBlock!=nil) failBlock(); return;

}

//方法二  通话结束后留在自己的应用界面

/**

*  UIWebView*callWebview =[[UIWebView alloc] init];

*  NSURL *telURL =[NSURL URLWithString:@"tel:10010"];

*  [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];

*  [self.view addSubview:callWebview];

*/

SimpleCallViewController *mediaVC=[[SimpleCallViewController alloc] init];

mediaVC.view.frame=CGRectZero;

mediaVC.view.alpha=.0f;

[vc addChildViewController:mediaVC];

[vc.view addSubview:mediaVC.view];

NSURLRequest *request=[NSURLRequest requestWithURL:url];

[mediaVC.webView loadRequest:request];

//  方法三 不一定通过App store

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10010"]];

}


+(void)sendMessage:(NSString *)message numb:(NSArray *)numberArr inViewController:(UIViewController *)vc failBlock:(void(^)())failBlock{

//    方法一 发送固定短信

NSString *tel = [NSString stringWithFormat:@"sms://%@",numberArr];

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:tel]];//发短信

//    方法二 发送任意短信

if ([MFMessageComposeViewController canSendText]) {

MFMessageComposeViewController *messageVc = [[MFMessageComposeViewController alloc] init];

messageVc.recipients = numberArr;  //支持群发

messageVc.body = message;          //短信内容

messageVc.messageComposeDelegate = self;

[vc presentViewController:messageVc animated:YES completion:nil];

[[[[messageVc viewControllers] lastObject] navigationItem] setTitle:@"测试短信"];//修改短信界面标题

}

else

NSLog(@"can't send message");

}

#pragma mark ****** MFMessageComposeViewControllerDelegate ******

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{

[controller dismissViewControllerAnimated:NO completion:nil];//关键的一句  不能为YES

switch ( result ) {

case MessageComposeResultCancelled:

NSLog(@"发送取消");

break;

case MessageComposeResultFailed:// send failed

NSLog(@"发送成功");

break;

case MessageComposeResultSent:

NSLog(@"发送失败");

break;

default:

break;

}

}

/** *  发邮件 */+ (void)sendMailInViewController:(UIViewController *)vc failBlock:(void(^)())failBlock;{    

Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); 

if (mailClass != nil) 

   {

        if ([mailClass canSendMail])

        { 

           MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 

           //    字体颜色 

           //    mc.navigationBar.tintColor = [UIColor colorWithRed:209.0/255 green:183.0/255 blue:126.0/255 alpha:1.0];

            //    代理 

           mc.mailComposeDelegate = self;

           //    主题

            [mc setSubject:@"Hello, World!"];

            //    主送

            [mc setToRecipients:[NSArray arrayWithObjects:@"502748959@qq.com", nil]];

            //    抄送

            [mc setCcRecipients:[NSArray arrayWithObject:@"wenfuhoo@163.com"]]; 

           //    密送

            [mc setBccRecipients:[NSArray arrayWithObject:@"1945028197@qq.com"]];

            //    内容 1

            [mc setMessageBody:@"Watson!!!\n\nCome here, I need you!" isHTML:NO];

            //    内容 2

            // [mc setMessageBody:@"Hello, Joe! What do you know?"  isHTML:YES]; 

           //    内容 3 

           //            [mailCompose setMessageBody:[self formalEmailBody] isHTML:YES]; 

           // 添加图片

            UIImage *addPic = [UIImage imageNamed:@""];

            NSData *imageData = UIImagePNGRepresentation(addPic);            // png            

        // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg 

             [mc addAttachmentData: imageData mimeType: @"" fileName: @""];    

              [vc presentViewController:mc animated:YES completion:nil];   

    }  

      else

        {

           NSLog(@"无法发送邮件,请在“邮箱”应用中设置您的邮箱帐户!");

        } 

   }

}

#pragma mark ****** MFMailComposeViewControllerDelegate ******

- (void)mailComposeController:(MFMailComposeViewController*)controller

didFinishWithResult:(MFMailComposeResult)result

error:(NSError*)error {

switch (result)

{

case MFMailComposeResultCancelled:

NSLog(@"Mail send canceled...");

break;

case MFMailComposeResultSaved:

NSLog(@"Mail saved...");

break;

case MFMailComposeResultSent:

NSLog(@"Mail sent...");

break;

case MFMailComposeResultFailed:

NSLog(@"Mail send errored: %@...", [error localizedDescription]);

break;

default:

break;

}

[controller dismissViewControllerAnimated:YES completion:nil];

}

- (NSString *)formalEmailBody

{

NSString *strBody;

/// 读取feedback.html文件

NSString *fdFile = [[NSBundle mainBundle] pathForResource:@"feedback"

ofType:@"html"];

NSString *strFeedBack = [NSString stringWithContentsOfFile:fdFile

encoding:NSUTF8StringEncoding

error:nil];

if (strFeedBack.length > 0){

/// 获取APP名称

NSDictionary *appInfo = [[NSBundle mainBundle] localizedInfoDictionary];

NSString *appName = [appInfo objectForKey:@"CFBundleDisplayName"];

/// 获取APP版本号

NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

/// 获取设备类型及OS版本

NSString *devName = [UIDevice currentDevice].model;

NSString *sysVersion = [UIDevice currentDevice].systemVersion;

strBody = [NSString stringWithFormat:strFeedBack,devName,sysVersion,appName,appVersion];

}

else

strBody = @"";

return strBody;

}

-(void)dealloc

{

self.webView=nil;

}

推荐阅读更多精彩内容