一个关于iphone应用邮件功能的问题
2010-11-15 18:13
369 查看
邮件功能也许没有太多需要说的,这里要说的是有关邮件功能涉及到的一些细节问题。先看一段代码:
- (void)LaunchMailComposer:(UIButton *)sender
{
NSArray *Array = [ImageArray objectAtIndex:sender.tag-1];
NSString *URL = [Array objectAtIndex:1];
MFMailComposeViewController *Composer = [[MFMailComposeViewController alloc] init];
//Composer = [Composer init];
NSLog(@"composer description:%@",[Composer description]);
Composer.mailComposeDelegate = self;
//NSArray *toRecipients = nil;
// NSArray *ccRecipients = nil;
// NSArray *bccRecipients = nil;
// [Composer setToRecipients:toRecipients];
// [Composer setCcRecipients:ccRecipients];
// [Composer setBccRecipients:bccRecipients];
NSString *Body = [NSString stringWithFormat:@"<style type='text/css'><!-- body{background-image:url(%@)} --></style>", URL];
//NSString *Body = @"<style type='text/css'><!-- body{background-image:url(http://www.4oa.com/Article/UploadPic/2009-4/2009429102347901.jpg)} --></style>";
[Composer setMessageBody:Body isHTML:YES];
if (nil == Composer) {
return;
}
[self presentModalViewController:Composer animated:YES];
[Composer release];
}
上面的代码本身并没有什么问题,在通常来说也不会有什么问题,但是做一个有趣的操作,把所有的默认设置好的邮箱都删除,奇怪的事情出现了,如果仅仅是上面的方式来设置邮件功能回导致程序崩溃。认真调试,怎么会初始化失败呢?原来,忘记了是否能发送邮件。那么我们在看如下的代码:
- (void)LaunchMailComposer:(UIButton *)sender
{
NSArray *Array = [ImageArray objectAtIndex:sender.tag-1];
NSString *URL = [Array objectAtIndex:1];
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Please set your mail address!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
return;
}
MFMailComposeViewController *Composer = [[MFMailComposeViewController alloc] init];
//Composer = [Composer init];
NSLog(@"composer description:%@",[Composer description]);
Composer.mailComposeDelegate = self;
//NSArray *toRecipients = nil;
// NSArray *ccRecipients = nil;
// NSArray *bccRecipients = nil;
// [Composer setToRecipients:toRecipients];
// [Composer setCcRecipients:ccRecipients];
// [Composer setBccRecipients:bccRecipients];
NSString *Body = [NSString stringWithFormat:@"<style type='text/css'><!-- body{background-image:url(%@)} --></style>", URL];
//NSString *Body = @"<style type='text/css'><!-- body{background-image:url(http://www.4oa.com/Article/UploadPic/2009-4/2009429102347901.jpg)} --></style>";
[Composer setMessageBody:Body isHTML:YES];
if (nil == Composer) {
return;
}
[self presentModalViewController:Composer animated:YES];
[Composer release];
}
其实就只是多了一个判断条件这样就好了。可是我们如果想要在没有设置邮件帐号的情况下进入到设置页面呢?
那么我们可以用如下方式:
-(IBAction)emailBTN:(id)sender
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != Nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
#pragma mark -
#pragma mark Compose Mail
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:[imageNameArray objectAtIndex:picNumber]];//@"Hello from California!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"fengxijun51020@hotmail.com"];
NSArray *ccRecipients = nil;//[NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
NSArray *bccRecipients = nil;//[NSArray arrayWithObject:@"fourth@example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:[imageNameArray objectAtIndex:picNumber] ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpg" fileName:[imageNameArray objectAtIndex:picNumber]];
// Fill out the email body text
NSString *emailBody = nil;//[imageNameArray objectAtIndex:picNumber];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
[self dismissModalViewControllerAnimated:YES];
break;
case MFMailComposeResultSaved:
[self sendResult:@"/n/nE-mail saved!"];
break;
case MFMailComposeResultSent:
[self sendResult:@"/n/nSent succeed!"];
break;
case MFMailComposeResultFailed:
[self sendResult:@"send Failed!"];
break;
}
}
-(void)sendResult:(NSString *)string
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[self performSelector:@selector(delayDismissModalView:) withObject:alert afterDelay:1];
}
-(void)delayDismissModalView:(UIAlertView *)alert
{
[alert dismissWithClickedButtonIndex:0 animated:YES];
[self dismissModalViewControllerAnimated:YES];
[alert release];
}
#pragma mark -
#pragma mark Workaround
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
NSString *body = @"&body=It is raining in sunny California!";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
这样邮件功能就做好了。
小提示:在写代码是一定要逻辑严谨,否则出现了问题是很难调试的,特别是与人合作进行开发的过程中这点尤其重要。
- (void)LaunchMailComposer:(UIButton *)sender
{
NSArray *Array = [ImageArray objectAtIndex:sender.tag-1];
NSString *URL = [Array objectAtIndex:1];
MFMailComposeViewController *Composer = [[MFMailComposeViewController alloc] init];
//Composer = [Composer init];
NSLog(@"composer description:%@",[Composer description]);
Composer.mailComposeDelegate = self;
//NSArray *toRecipients = nil;
// NSArray *ccRecipients = nil;
// NSArray *bccRecipients = nil;
// [Composer setToRecipients:toRecipients];
// [Composer setCcRecipients:ccRecipients];
// [Composer setBccRecipients:bccRecipients];
NSString *Body = [NSString stringWithFormat:@"<style type='text/css'><!-- body{background-image:url(%@)} --></style>", URL];
//NSString *Body = @"<style type='text/css'><!-- body{background-image:url(http://www.4oa.com/Article/UploadPic/2009-4/2009429102347901.jpg)} --></style>";
[Composer setMessageBody:Body isHTML:YES];
if (nil == Composer) {
return;
}
[self presentModalViewController:Composer animated:YES];
[Composer release];
}
上面的代码本身并没有什么问题,在通常来说也不会有什么问题,但是做一个有趣的操作,把所有的默认设置好的邮箱都删除,奇怪的事情出现了,如果仅仅是上面的方式来设置邮件功能回导致程序崩溃。认真调试,怎么会初始化失败呢?原来,忘记了是否能发送邮件。那么我们在看如下的代码:
- (void)LaunchMailComposer:(UIButton *)sender
{
NSArray *Array = [ImageArray objectAtIndex:sender.tag-1];
NSString *URL = [Array objectAtIndex:1];
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Please set your mail address!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
return;
}
MFMailComposeViewController *Composer = [[MFMailComposeViewController alloc] init];
//Composer = [Composer init];
NSLog(@"composer description:%@",[Composer description]);
Composer.mailComposeDelegate = self;
//NSArray *toRecipients = nil;
// NSArray *ccRecipients = nil;
// NSArray *bccRecipients = nil;
// [Composer setToRecipients:toRecipients];
// [Composer setCcRecipients:ccRecipients];
// [Composer setBccRecipients:bccRecipients];
NSString *Body = [NSString stringWithFormat:@"<style type='text/css'><!-- body{background-image:url(%@)} --></style>", URL];
//NSString *Body = @"<style type='text/css'><!-- body{background-image:url(http://www.4oa.com/Article/UploadPic/2009-4/2009429102347901.jpg)} --></style>";
[Composer setMessageBody:Body isHTML:YES];
if (nil == Composer) {
return;
}
[self presentModalViewController:Composer animated:YES];
[Composer release];
}
其实就只是多了一个判断条件这样就好了。可是我们如果想要在没有设置邮件帐号的情况下进入到设置页面呢?
那么我们可以用如下方式:
-(IBAction)emailBTN:(id)sender
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != Nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
#pragma mark -
#pragma mark Compose Mail
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:[imageNameArray objectAtIndex:picNumber]];//@"Hello from California!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"fengxijun51020@hotmail.com"];
NSArray *ccRecipients = nil;//[NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
NSArray *bccRecipients = nil;//[NSArray arrayWithObject:@"fourth@example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:[imageNameArray objectAtIndex:picNumber] ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpg" fileName:[imageNameArray objectAtIndex:picNumber]];
// Fill out the email body text
NSString *emailBody = nil;//[imageNameArray objectAtIndex:picNumber];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
[self dismissModalViewControllerAnimated:YES];
break;
case MFMailComposeResultSaved:
[self sendResult:@"/n/nE-mail saved!"];
break;
case MFMailComposeResultSent:
[self sendResult:@"/n/nSent succeed!"];
break;
case MFMailComposeResultFailed:
[self sendResult:@"send Failed!"];
break;
}
}
-(void)sendResult:(NSString *)string
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[self performSelector:@selector(delayDismissModalView:) withObject:alert afterDelay:1];
}
-(void)delayDismissModalView:(UIAlertView *)alert
{
[alert dismissWithClickedButtonIndex:0 animated:YES];
[self dismissModalViewControllerAnimated:YES];
[alert release];
}
#pragma mark -
#pragma mark Workaround
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
NSString *body = @"&body=It is raining in sunny California!";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
这样邮件功能就做好了。
小提示:在写代码是一定要逻辑严谨,否则出现了问题是很难调试的,特别是与人合作进行开发的过程中这点尤其重要。
相关文章推荐
- 关于邮件列表一个问题的解释
- 一个“解决一个第三方应用一个功能打不开的问题”思路。
- 一个小小的简单计算器问题,有关于栈的应用
- 关于邮件列表一个问题的解释
- 关于ACCESS中TOP 功能的一个问题
- 关于Qt开发iPhone/iPad和Android环境下的应用的一些问题
- 关于开发Android应用时写从图库选择头像功能时遇到的一些问题。
- 关于注册表应用我有一个问题,望不吝赐教,谢谢
- php的IMAP的应用实例(实现收发邮件、删除邮件、附件下载等功能,解决中文乱码问题)
- iPhone应用中关于NSTimer的一些问题
- 遇到的一个关于功能IO口的问题
- 一个关于Media Player 跨iPhone版本的问题
- 最近遇到一个非常棘手的需求是关于邮件转发的问题,请大家来帮忙给出出主意如何实现。
- 一个关于在VB.NET中应用超级解霸的问题
- 一个数组变换的算法问题-关于前端的分页功能排序(点击/热门)功能
- 2017-6-14 关于使用history.go的问题!配合原生应用嵌入H5页面(只有一个webview的的单页应用)返回历史列表记录的问题
- 关于一个应用分枝界限法解决最小顶点覆盖问题的程序说明
- 一个关于邮件大小的有趣的问题
- 关于iPhone的一个广告加载问题
- 一个关于Media Player 跨iPhone版本的问题