使用 openURL 发送邮件的问题

发布于 2024-11-10 13:22:04 字数 515 浏览 7 评论 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]];

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

谢谢

I'm facing a issue with opening mail client using openURL. Here is the code.

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]];

I guess, is there any kind of encoding needed to use special characters, which is do present, but not shown here in the sample text.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱情眠于流年 2024-11-17 13:22:04
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]];
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]];
っ左 2024-11-17 13:22:04

我想你可以用这个

    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

I think you can use this

    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];

instead of openURL

岛徒 2024-11-17 13:22:04

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

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

,即

[yourMailPicker setMessageBody:body isHTML:YES];

I think you can make use of MFMailComposeViewController for this. That can help you to support special characters by using this method

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

ie

[yourMailPicker setMessageBody:body isHTML:YES];
哑剧 2024-11-17 13:22:04

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

tried your code and got the problem [NSURL URLWithString:urlString] this line returns nil.

涫野音 2024-11-17 13:22:04
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];
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]];

I change in this line check it, and compare it...

 NSString *urlString = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@",subject,body];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文