0

我在使用 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]];

我想,是否有任何类型的编码需要使用特殊字符,这是确实存在的,但在示例文本中没有显示。

谢谢

4

5 回答 5

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]];
于 2011-05-30T06:23:10.623 回答
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

于 2011-05-30T06:15:09.263 回答
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];
于 2011-05-30T06:13:03.417 回答
0

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

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

IE

[yourMailPicker setMessageBody:body isHTML:YES];
于 2011-05-30T06:15:27.877 回答
0

尝试了您的代码并遇到了问题 [NSURL URLWithString:urlString] 此行返回 nil。

于 2011-05-30T06:20:15.570 回答