问题描述
|
我在使用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]];
我想,是否存在使用特殊字符的任何编码方式,这种编码方式确实存在,但示例文本中未在此处显示。
谢谢
解决方法
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]];
, 我想你可以用这个
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
, 我想您可以为此使用MFMailComposeViewController
。使用此方法可以帮助您支持特殊字符
- (void)setMessageBody:(NSString*)body isHTML:(BOOL)isHTML
即
[yourMailPicker setMessageBody:body isHTML:YES];
, 尝试了您的代码,并出现了问题[NSURL URLWithString:urlString],此行返回nil。
, 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=%@\",body];