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文件
- #import <UIKit/UIKit.h>
- #import <MessageUI/MessageUI.h>
- @interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>
- -(IBAction)sendMail:(id)sender;
- @end
4.在MainStoryboard.storyboard文件中添加一个Round Rect Button ,关联到sendMail 函数。
5.在ViewController.m文件中添加如下方法
- -(IBAction)sendMail:(id)sender
- {
- if ([MFMailComposeViewController canSendMail])
- {
- MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
- mail.mailComposeDelegate = self;
- [mail setSubject:@"Hi here is Subject!"];
- NSArray *toRecipients = [NSArray arrayWithObjects:@"justcoding@gmail.com", nil];
- [mail setToRecipients:toRecipients];
- [mail setCcRecipients:toRecipients];
- NSString *emailBody = @"This is a test email !";
- [mail setMessageBody:emailBody isHTML:NO];
- mail.modalPresentationStyle = UIModalPresentationFormSheet;
- [self presentModalViewController:mail animated:YES];
- }
- else
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Email is not supported on your device" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
- [alert show];
- }
- }
- -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
- {
- switch (result)
- {
- case MFMailComposeResultCancelled:
- NSLog(@"Cancelled");
- break;
- case MFMailComposeResultSaved:
- NSLog(@"Message saved in drafts folder");
- break;
- case MFMailComposeResultSent:
- NSLog(@"Message is sent");
- break;
- case MFMailComposeResultFailed:
- NSLog(@"Message not sent due to error");
- break;
- default:
- NSLog(@"Mail not sent");
- break;
- }
- [self dismissModalViewControllerAnimated:YES];
- }
视频地址:http://www.youtube.com/watch?v=ECkJh2mnFc8&feature=player_embedded