2011-05-30 84 views
0

我正在使用openURL打开邮件客户端时遇到问题。这是代码。使用openURL发送邮件的问题

NSString *subject = @"Demo Subject"; 
NSString *body = @"<html><head>Header</head><body><a href=\"http://example.com\">Here is the demo link</a></body></html>"; 
NSString *urlString = [NSString stringWithFormat:@"mailto:?&subject=%@&body=%@",subject,body]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; 

我想,没有任何一种使用特殊字符需要进行编码,这是目前做的,而不是在示例文本显示在这里。

感谢

回答

5
NSString *htmlBody = @"you probably want something HTML-y here"; 

// First escape the body using a CF call 
NSString *escapedBody = [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)htmlBody, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease]; 

// Then escape the prefix using the NSString method 
NSString *mailtoPrefix = [@"mailto:?subject=Some Subject&body=" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

// Finally, combine to create the fully escaped URL string 
NSString *mailtoStr = [mailtoPrefix stringByAppendingString:escapedBody]; 

// And let the application open the merged URL 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailtoStr]]; 
+0

Chetu,thanx,它的工作......在这里,我只是尝试了openURL的整个urlString的NSUTF8StringEncoding。如果使用CFURLCreateStringByAddingPercentEscapes,它会起什么作用吗? – illuminatus 2011-05-30 06:46:20

0
NSString *subject = @"Demo Subject"; 
NSString *body = @"<html><head>Header</head><body><a href=\"http://example.com\">Here is the demo link</a></body></html>"; 
NSString *urlString = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@",subject,body]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; 

我在这一行检查,并比较其更改...

NSString *urlString = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@",subject,body]; 
+1

你做了哪些改变? – 2011-05-30 06:25:10

1

我认为你可以使用这个

MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init]; 
    [[mail navigationBar] setTintColor:[UIColor blackColor]]; 
    mail.mailComposeDelegate = self; 
    if([MFMailComposeViewController canSendMail]) 
    { 
     [mail setSubject:@"Demo Subject"]; 
     [mail setToRecipients:[NSArray arrayWithObject:@"me"]]; 
     [mail setMessageBody:@"<html><head>Header</head><body><a href=\"http://example.com\">Here is the demo link</a></body></html>" isHTML:YES]; 
     [self presentModalViewController:mail animated:YES]; 
    } 
    [mail release]; 

代替的OpenURL

+0

thanx的回应,我用这种方式,但因为我们必须明确地解雇邮件作曲家,我只是尝试一种替代方式。 – illuminatus 2011-05-30 06:22:18

0

我想你ca n为此使用MFMailComposeViewController。这可以帮助您使用此方法

- (void)setMessageBody:(NSString*)body isHTML:(BOOL)isHTML 

支持特殊字符即

[yourMailPicker setMessageBody:body isHTML:YES]; 
0

想你的代码,并得到了该问题[NSURL URLWithString:urlString]这条线返回零。

+0

是的,它就是我的问题,正如@Love Chetu所说的,只需按照步骤对其进行编码即可。我更喜欢使用CFURLCreateStringByAddingPercentEscapes作为主体和主体,然后设置urlString。 – illuminatus 2011-06-01 04:33:45