2

我在使用我的應用程序中的郵件發送功能。在從我的應用發送郵件的同時,在Gmail應用的郵件正文中。它不顯示&'這是特殊字符。在發送郵件時,無法在郵件正文中顯示&(&符號)

enter image description here

這裏是代碼。

//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

你制定機構與'isHTML:NO'參數? – alexburtnik

+0

@alexburtnik我使用[[UIApplication sharedApplication] openURL:emailUrlFinal]發送郵件; 在這裏,我無法找到這樣的選項。 –

+0

用完整的代碼示例來編輯你的問題,你如何做到這一點。 「emailUrlFinal」究竟是什麼? – alexburtnik

回答

0

使用下面的方法從字符串中替換特殊字符。

有特殊字符傳遞你的字符串以下方法:

-(NSString*)replaceSpecialCharsFromString:(NSString*)str 
{ 
    str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"]; 
    str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"]; 
    str = [str stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"]; 
    str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"&#39;"]; 

    return str; 
} 

現在,在郵件正文中使用str並檢查它是否解決您的問題。

+0

不工作。 :( –

0

您應該使用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:@[@"[email protected]"]]; 
     [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 
+0

它在默認的郵件發送功能中工作正常 –

+0

如果工作正常,你不會問一個關於不工作的問題'&',對嗎?問題是你使用的方法不是「默認」就像你說的,而且如果有一個設備上沒有谷歌的郵件應用程序會失敗,而這是我張貼的默認方法會奏效。 – alexburtnik

+0

我的問題包括「在Gmail應用程序的消息體」,這闡明它不工作的Gmail應用程序。我已經安裝了gmail和它的開放,但編碼也不適用於&和'。並同時添加%26和%E2%80%99 –