我在我的应用中处理邮件发送功能。在我的应用程序发送邮件时,Gmail应用程序的邮件正文中。它没有显示&
和'
这些特殊字符。
这是代码。
//Suject
NSString *strSubject = @"Subjet";
// Message Body
__block NSMutableString *messageBody = [[NSMutableString alloc] init];
// gmmmailto
//googlegmail:///co?subject=<subject text>&body=<body text>
NSURL *checkGmailAppURL = [NSURL URLWithString:[@"googlegmail:///co?to=&subject=hi&body=hi" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL:checkGmailAppURL])
{
[messageBody appendFormat:@"%@\n\n", @"Hi I want sell this application & want be free"];
NSString *urlFinal = [NSString stringWithFormat: @"googlegmail:///co?subject=%@&body=%@", strSubject, messageBody];
NSURL *emailUrlFinal = [NSURL URLWithString:[urlFinal stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
[[UIApplication sharedApplication] openURL:emailUrlFinal];
}
答案 0 :(得分:0)
使用以下方法替换字符串中的特殊字符。
将带有特殊字符的字符串传递给下面的方法:
-(NSString*)replaceSpecialCharsFromString:(NSString*)str
{
str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
str = [str stringByReplacingOccurrencesOfString:@">" withString:@">"];
str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
return str;
}
现在,在邮件正文中使用str
并检查它是否可以解决您的问题。
答案 1 :(得分:-1)
您应该使用MFMailComposeViewController
,这是专为从应用发送电子邮件而设计的。以下是如何使用它的代码示例:
#import "ViewController.h"
#import <MessageUI/MFMailComposeViewController.h>
@interface ViewController () <MFMailComposeViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self sendEmail];
}
- (void) sendEmail {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController* mailController = [[MFMailComposeViewController alloc] init];
mailController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
mailController.mailComposeDelegate = self;
[mailController setSubject:NSLocalizedString(@"Feedback", @"")];
[mailController setTitle:NSLocalizedString(@"Feedback", @"")];
[mailController setToRecipients:@[@"example@gmail.com"]];
[mailController setMessageBody:@"Hello & ampersand" isHTML:NO];
if (mailController) {
[self presentViewController:mailController animated:YES completion:nil];
}
}
else {
//show UIAlertView
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
//Handle completion
}
@end
&
或'
修改强>
如果您仍想使用gmail应用程序,请尝试以下代码,它对我有用:
NSString *messageBodyRaw = @"Hi I want sell this application & want be free\n\n";
NSString *messageBody = [messageBodyRaw urlEncodeUsingEncoding:NSUTF8StringEncoding];
NSString *urlFinal = [NSString stringWithFormat: @"googlegmail:///co?subject=%@&body=%@", strSubject, messageBody];
NSURL *emailUrlFinal = [NSURL URLWithString:urlFinal];
[[UIApplication sharedApplication] openURL:emailUrlFinal];
的NSString + URLEncoding.h:
#import <Foundation/Foundation.h>
@interface NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
@end
的NSString + URLEncoding.m:
#import "NSString+URLEncoding.h"
@implementation NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
CFStringConvertNSStringEncodingToEncoding(encoding)));
}
@end