微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用openURL发送邮件时出现问题

如何解决使用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]];
我想,是否存在使用特殊字符的任何编码方式,这种编码方式确实存在,但示例文本中未在此处显示。 谢谢     

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。