IOS开发之(应用中添加发送邮件功能)

By | 2014/06/05
版权所有转载请注明,本文转自:http://www.androiddev.net/author/wildcat/
1.在Xcode中创建一个新的项目,选择Single View Application然后点击next.

  de70a68f-e709-4d16-9e1d-e2c13e2656aa

2.设置项目名为EmailDemo然后创建
3.进入项目后 ,进入ViewController.m
把下面代码放到 viewDidLoad 方法后
#pragma mark 点击发送按钮
– (IBAction)emailSupport:(id)sender
{
NSLog(@“Clicked”);
}
 
4.点击Main.storyboard ,拖拽一个UIButton放到视图中央
5.UIButton与刚才我们建的函数简历链接,如下图:
5794e80e-29e3-45f4-832d-3582ada97135
 
6.添加MessageUI.framework,如下图:
6a4321ad-0841-4245-9ccc-4e2a0f262999
 
 
7.再回到ViewController.m文件 添加引用
#import <MessageUI/MessageUI.h>
给ViewController添加协议:

@interface ViewController ()<MFMailComposeViewControllerDelegate>

@end

 
8.修改– (IBAction)emailSupport:(id)sender方法:

[code lang=”objc”]
#pragma mark 点击发送按钮
– (IBAction)emailSupport:(id)sender
{
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
NSString *model = [[UIDevice currentDevice] model];
//1.从plist中获得版本号
NSString *key=(NSString *)kCFBundleVersionKey;
NSString *version=[NSBundle mainBundle].infoDictionary[key];

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:[NSArray arrayWithObjects:@"lveslxl@163.com",nil]];

[mailComposer setSubject:[NSString stringWithFormat: @"意见反馈( V%@ )",version]];
NSString *supportText = [NSString stringWithFormat:@"Device: %@\niOS Version:%@\n\n",model,iOSVersion];
supportText = [supportText stringByAppendingString: @"意见反馈,畅所欲言吧."];
[mailComposer setMessageBody:supportText isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
}

[/code]

9.实现 MFMailComposeViewControllerDelegate代理方法

[code lang=”objc”]
– (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{

switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}

// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];

}

[/code]

 
 
好了一切就绪点击运行,结果如下:
屏幕快照 2014-06-05 下午7.48.46 屏幕快照 2014-06-05 下午7.49.54
版权所有转载请注明,本文转自:http://www.androiddev.net/author/wildcat/

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据