使用变量将变量调用到应用内电子邮件中

时间:2011-02-26 03:37:29

标签: objective-c cocoa-touch ios email nsuserdefaults

嘿大家,我正在尝试做的是从我的界面上的文本字段中获取变量,并在由这些按钮正下方的按钮触发的电子邮件中使用它们。我按照教程向我展示了如何做到这一点,使用.html文件格式化,我无法在文件中实际替换这些占位符!如果有人可以请告诉我我做错了什么,或者会有什么效果值得赞赏!

以下是点击电子邮件按钮

的方法中实现文件的代码
- (IBAction)email:(id)sender {
    MFMailComposeViewController *compose = [[MFMailComposeViewController alloc] init];
    compose.mailComposeDelegate = self;

    [compose setSubject:@"Float Plan for my Boat Trip"];
    [compose setCcRecipients:[NSArray arrayWithObject:@"towboat911@gmail.com"]];

    NSUserDefaults *person = [NSUserDefaults standardUserDefaults];
    [person setObject:personField.text forKey:@"person"];

    NSUserDefaults *pob = [NSUserDefaults standardUserDefaults];
    [pob setObject:pobField.text forKey:@"pob"];

    NSUserDefaults *phone = [NSUserDefaults standardUserDefaults];
    [phone setObject:phoneField.text forKey:@"phone"];

    NSUserDefaults *medical = [NSUserDefaults standardUserDefaults];
    [medical setObject:medicalField.text forKey:@"medical"];

    NSUserDefaults *depart = [NSUserDefaults standardUserDefaults];
    [depart setObject:departField.text forKey:@"depart"];

    NSUserDefaults *left = [NSUserDefaults standardUserDefaults];
    [left setObject:leftField.text forKey:@"left"];

    NSUserDefaults *arrive = [NSUserDefaults standardUserDefaults];
    [arrive setObject:arriveField.text forKey:@"arrive"];

    NSUserDefaults *destination = [NSUserDefaults standardUserDefaults];
    [destination setObject:destinationField.text forKey:@"destination"];


    NSString *emailFileName = [NSString stringWithFormat:@"email_en_US.html"];
    NSString *emailFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: emailFileName];
    NSString *body = [NSString stringWithContentsOfFile:emailFilePath encoding:NSUTF8StringEncoding error:nil];

    body = [body stringByReplacingOccurrencesOfString:@"//PERSON//" withString:@"person"];
    body = [body stringByReplacingOccurrencesOfString:@"//PERSONSIGN//" withString:@"person"];
    body = [body stringByReplacingOccurrencesOfString:@"//POB//" withString:@"pob"];
    body = [body stringByReplacingOccurrencesOfString:@"//PHONE//" withString:@"phone"];
    body = [body stringByReplacingOccurrencesOfString:@"//MEDICAL//" withString:@"medical"];
    body = [body stringByReplacingOccurrencesOfString:@"//DEPART//" withString:@"depart"];
    body = [body stringByReplacingOccurrencesOfString:@"//LEFT//" withString:@"left"];
    body = [body stringByReplacingOccurrencesOfString:@"//ARRIVAL//" withString:@"arrive"];
    body = [body stringByReplacingOccurrencesOfString:@"//DESTINATION//" withString:@"destination"];

    NSString *emailBody = [NSString stringWithFormat:body];
    [compose setMessageBody:emailBody isHTML:YES];

    compose.navigationBar.barStyle = UIBarStyleBlack;

    [self presentModalViewController:compose animated:YES];
    [compose release];

}

1 个答案:

答案 0 :(得分:0)

替换正文中的字符串的行看起来不正确。例如,这个:

body = [body stringByReplacingOccurrencesOfString:@"//PERSON//" 
           withString:@"person"];

应该是这样的:

body = [body stringByReplacingOccurrencesOfString:@"//PERSON//" 
           withString:personField.text];

其余的stringByReplacingOccurrencesOfString行也是如此。


此外,您不需要为要保存的每个键创建单独的NSUserDefaults引用。因此,不要声明NSUserDefaults *personNSUserDefaults *pob等,而是声明一个类似NSUserDefaults *defaults的引用,并在所有setObject调用上使用它。