Issue with mail sending using openURL

China☆狼群 提交于 2019-12-06 14:58:51

问题


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


回答1:


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



回答2:


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




回答3:


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



回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/6172664/issue-with-mail-sending-using-openurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!