如何动态地将电子邮件正文传递给MFMailComposeViewController

时间:2012-05-07 23:11:33

标签: objective-c ios

大家好,感谢您的时间。我相信我有一个简单的问题。

我试图剥离它的引号的字符串电子邮件正文(它们似乎来自于从字典中获取我的正文)并将其传递给MailObject ....没有运气我继续在我的*上获得SIGBRT finalString线。
我错过了什么再次感谢任何帮助。

NSMutableDictionary *my_data = [myMasterOrderList objectAtIndex:[self.tableView indexPathForCell:cell].row];
   NSMutableArray   *toEmail = [[NSMutableArray alloc] init];
   [toEmail addObject:[my_data objectForKey:@"repemail"]];

   NSMutableString *toBody = [my_data objectForKey:@"myOrder"];
  // [toBody addObject:[my_data objectForKey:@"myOrder"]];
  // NSString *finalSting = [[NSString alloc] init];
   NSMutableString *finalString = [NSMutableString stringWithFormat:@"%@",[toBody stringByReplacingOccurrencesOfString:@"\"" withString:@"%20"]];

   NSLog(@" toBody my_data%@", finalString);
//*************  SEND MAIL *************
   if ([MFMailComposeViewController canSendMail]) {

       MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
       mailViewController.mailComposeDelegate = self;
       [mailViewController setToRecipients:toEmail];
       [mailViewController setSubject:@"Sub Goes Here."];
       [mailViewController setMessageBody:finalString isHTML:NO];

       [self presentModalViewController:mailViewController animated:YES];

2 个答案:

答案 0 :(得分:2)

我认为问题是:withString:@"%20"它必须是withString:@"%%20"因为%字符在NSStrings中是特殊的,要有文字%符号,你需要将其转义。

我还要注意,如果你不需要finalString是可变的(从你发布的代码,你没有),你可以这样做:

NSString *finalString = [toBody stringByReplacingOccurrencesOfString:@"\"" withString:@"%%20"];

答案 1 :(得分:0)

我测试了这段代码:

NSString *toBody = @"this \" is my \" body \" email\"";
NSMutableString *finalString = [NSMutableString stringWithFormat:@"%@",[toBody stringByReplacingOccurrencesOfString:@"\"" withString:@"%20"]];
NSLog(@"%@", finalString);

NSLog的输出是:

2012-05-07 19:44:04.157 StringTesting[19203:f803] this %20 is my %20 body %20 email%20

我实际上复制了您要使用的代码。我知道这听起来很基本,但是自从我的工作以来,您是否尝试过重新启动Xcode和/或重启机器,清理项目等等?