Проблема с отправкой почты с использованием 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 ответов
Решение
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
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];
Попробовал ваш код и получил проблему [NSURL URLWithString:urlString] эта строка возвращает ноль.
Я думаю, что вы можете использовать MFMailComposeViewController
за это. Это может помочь вам поддерживать специальные символы с помощью этого метода
- (void)setMessageBody:(NSString*)body isHTML:(BOOL)isHTML
т.е.
[yourMailPicker setMessageBody:body isHTML:YES];