Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5379530
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类:

2010-09-09 09:55:31

Phone SDK3.0后支持MFMailComposeViewControllerDelegate,所以在编写app时有两种方式给我们的email内容添加URL。
(1)方法一:MFMailComposeViewControllerDelegate,这个比较实用,不像调用opneURL函数后就自动退出当前运行的app了(难道iPhone不支持多线程运行吗?迷惑,不解)。下面这个例子以我自己的blog URL为例说明,用html格式写的,用html写的话灵活度比较大,什么URL都能加。

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

if (mailClass != nil)

{

/ We must always check whether the current device is configured for sending emails

  if ([mailClass canSendMail])

  {

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

     picker.mailComposeDelegate = self;

   [picker setSubject:NSLocalizedString(@"My blog!",nil)];

   // Set up recipients

   NSArray *toRecipients = [NSArray arrayWithObject:self.emailAddress];

    [picker setToRecipients:toRecipients];

   NSString *blogLink =[NSString stringWithFormat:

   @"my blog",@"zhoushu",8606];

   NSString *body=[NSString stringWithFormat:NSLocalizedString(@"This is test! Check   my blog with this link:%@",nil),blogLink];

   [picker setMessageBody:body isHTML:YES];

     [self presentModalViewController:picker animated:YES];

   [picker release];

}


(2)方法二:用text link方法打开email链接:-(void)openURL:@"mailto://";

用MFMailComposeViewControllerDelegate的另一个好处就是在给email的body添加的URL信息可以导入外来数字/字符串,而openURL方法添加时就会出现URL无法正确显示的问题?看到下面这个例子你就明白了。

 

NSString *toRecipients = self.emailAddress;

NSString *recipients = [NSString stringWithFormat:NSLocalizedString(@"mailto:%@?&subject=My location!",nil),toRecipients ];

NSString *googleMapLink =[NSString stringWithFormat: @"location"];

//上面这个链接能正确显示,但是如果要加载位置信息就无法在email中正确显示了,如 @">location"是没显示的,至少我自己编出来是这个样子。。。


NSString *body = [NSString stringWithFormat:NSLocalizedString(@"&body=My location is:%@(latitude=%f,longitude=%f,altitude=%f)",nil), googleMapLink, myLocation.latitude, myLocation.longitude,myLocation.altitude];

NSString *email=[NSString stringWithFormat:@"%@%@",recipients,body];

//URL一定要用NSUTF8StringEncoding方法编码才能用的哦~

email=[email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];



// 真机测试成功 

NSString *recive_user = [NSString stringWithFormat:@"YouFriend@mac.com"];        // 接收者地址
NSString *message_title = [NSString stringWithFormat:@"message title"];            // 邮件标题
NSString *http_link = [NSString stringWithFormat:@""]; // 超连接地址
NSString *messgae_content = [NSString stringWithFormat:@"&body= my test email %@", http_link]; // 发送的内容信息
NSString *email = [NSString stringWithFormat:@"mailto://%@?Subject=%@&body=%@", recive_user, message_title, messgae_content];
    
// 最终格式: @"mailto://YouFriend@mac.com?Subject=message title &body=message content"
//URL一定要用NSUTF8StringEncoding方法编码才能用的哦~
email=[email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];


阅读(2481) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~