开发者

Issue with mail sending using openURL

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&开发者_如何学Gogt;<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


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


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


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


tried your code and got the problem [NSURL URLWithString:urlString] this line returns 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]];

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

 NSString *urlString = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@",subject,body];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜