发邮件 In App Email (实例)

In App Email

In this tutorial i will be showing you how to directly email from your app Features:

 

  • 1 Round Rect Button 
In app emailing is a great feature for any developer and can be used to gather instant feedback from the customer or even a way to share your apps content improving the app and reaching a higher audience


The Code

Play the video to get a step by step walkthrough and all code can be copy and pasted 

 

1.新建立一个项目,view based application(with storyboard.ARC)

 

2.需要引入UIMessage Library (Build Phases => Link Binary With Libraries => +(添加) => MessageUI.framework(可以搜索查找))

 

3.编辑ViewController.h文件

 

C代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2. #import <MessageUI/MessageUI.h>  
  3. @interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>  
  4.   
  5. -(IBAction)sendMail:(id)sender;  
  6. @end  

 

4.在MainStoryboard.storyboard文件中添加一个Round Rect Button ,关联到sendMail 函数。

 

5.在ViewController.m文件中添加如下方法

 

C代码   收藏代码
  1. -(IBAction)sendMail:(id)sender  
  2. {  
  3.     if ([MFMailComposeViewController canSendMail])   
  4.     {  
  5.         MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];  
  6.         mail.mailComposeDelegate = self;  
  7.         [mail setSubject:@"Hi here is Subject!"];  
  8.         NSArray *toRecipients = [NSArray arrayWithObjects:@"justcoding@gmail.com", nil];  
  9.         [mail setToRecipients:toRecipients];  
  10.         [mail setCcRecipients:toRecipients];  
  11.           
  12.         NSString *emailBody = @"This is a test email !";  
  13.           
  14.         [mail setMessageBody:emailBody isHTML:NO];  
  15.         mail.modalPresentationStyle = UIModalPresentationFormSheet;  
  16.         [self presentModalViewController:mail animated:YES];  
  17.     }   
  18.     else   
  19.     {  
  20.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Email is not supported on your device" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];  
  21.         [alert show];  
  22.     }  
  23. }  
  24. -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error  
  25. {  
  26.     switch (result)   
  27.     {  
  28.         case MFMailComposeResultCancelled:  
  29.             NSLog(@"Cancelled");  
  30.             break;  
  31.         case MFMailComposeResultSaved:  
  32.             NSLog(@"Message saved in drafts folder");  
  33.             break;  
  34.               
  35.         case MFMailComposeResultSent:  
  36.             NSLog(@"Message is sent");  
  37.             break;  
  38.               
  39.         case MFMailComposeResultFailed:  
  40.             NSLog(@"Message not sent due to error");  
  41.             break;  
  42.         default:  
  43.             NSLog(@"Mail not sent");  
  44.             break;  
  45.     }  
  46.     [self dismissModalViewControllerAnimated:YES];  
  47. }  
 

 

视频地址:http://www.youtube.com/watch?v=ECkJh2mnFc8&feature=player_embedded


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值