0

为了节省用户发送电子邮件的时间,用户将电子邮件地址保存在首选项页面中,该页面应在撰写电子邮件时预先填写收件人。(或者这就是我想要做的)这是我卡住的地方,我如何将我保存的字符串用于对象以预先填充收件人。

目前该字符串未预先填写收件人

保存在首选项页面中:

NSString *savecontents = _emailAddress.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savecontents forKey:@"saveEmail"];
[defaults synchronize];

在邮件视图演示文稿中阅读此处

- (IBAction)email:(id)sender {

NSString *savedValue = [[NSUserDefaults standardUserDefaults] <---------- saved email string
                        stringForKey:@"saveEmail"];



if ([MFMailComposeViewController canSendMail]) {

    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init]; 
    mail.mailComposeDelegate = self;

    NSArray *toRecipients = [NSArray arrayWithObject:savedValue]; <------- trying to get string here
    [mail setToRecipients:toRecipients];

    [mail setSubject:@"subject"];
    NSString *emailBody = [NSString stringWithFormat: @"text here"] ;
    [mail setMessageBody:emailBody isHTML:YES];

    mail.modalPresentationStyle = UIModalPresentationPageSheet;
    [self presentModalViewController:mail animated:YES];

}

到目前为止尝试过:

  NSArray *toRecipients = [NSString stringWithFormat:savedValue];
   [mail setToRecipients:toRecipients];

    NSArray *toRecipients = [NSString stringWithFormat:@"%@",savedValue];
   [mail setToRecipients:toRecipients];

谷歌、SO 和敲桌子的拳头

4

1 回答 1

1

你只需要这样:

if (savedValue.length) {
    [mail setToRecipients:@[ savedValue ]];
}

这使用现代的 Objective-C 数组语法。这将与以下内容相同:

if (savedValue.length) {
    NSArray *toRecipients = [NSArray arrayWithObject:saveValue];
    [mail setToRecipients:toRecipients];
}

您正在尝试为变量赋值的NSString代码NSArray

stringWithFormat另外,除非您确实需要格式化字符串,否则请不要使用。

于 2013-06-04T17:50:34.447 回答