使用openURL发送邮件的问题

时间:2011-05-30 05:44:08

标签: iphone html email 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 个答案:

答案 0 :(得分: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]];

答案 1 :(得分: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

答案 2 :(得分: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];

答案 3 :(得分:0)

我认为您可以使用MFMailComposeViewController。这可以帮助您使用此方法支持特殊字符

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

[yourMailPicker setMessageBody:body isHTML:YES];

答案 4 :(得分:0)

尝试了你的代码并得到了问题[NSURL URLWithString:urlString]这一行返回nil。