我正在尝试使用 iOS 应用程序发送邮件。
方法是这样的:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Reclutamento pompieri"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8ccddcbccf8dfd5d9d1d496dbd7d5" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
[picker setToRecipients:toRecipients];
testoMail = [NSMutableString stringWithFormat: @"This is a test"];
[picker setMessageBody:testoMail isHTML:YES]; //HTML!!!!!!
[self presentViewController:picker animated:YES completion:nil];
//*******************************************************************************************
// The mail compose view controller delegate method
- (void)mailComposeController:(MFMailComposeViewController *)controller //**ERROR STAYS HERE**
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES]
;
但是调试器说“使用了未声明的标识符“mailComposerController”
没明白什么意思。 你能帮我吗?
非常欣赏你
PS。标题是:#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface Reclutamento : UIViewController <MFMailComposeViewControllerDelegate>
{
}
- (void)sendMail;
@end
完整的.h代码是
//
// Reclutamento.h
// Pompieri
//
// Created by Reda Bousbah on 15.01.13.
// Copyright (c) 2013 Reda Bousbah. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface Reclutamento : UIViewController <MFMailComposeViewControllerDelegate>
{
UITextField *nameTextField;
UITextField *surnameTextField;
UITextField *bornDateTextField;
UITextField *addressTextField;
UITextField *zipTextField;
UITextField *emailTextField;
MFMailComposeViewController *email;
}
@property (nonatomic, retain) MFMailComposeViewController *email;
@property(nonatomic,retain) IBOutlet UITextField *nameTextField;
@property(nonatomic,retain) IBOutlet UITextField *surnameTextField;
@property(nonatomic,retain) IBOutlet UITextField *bornDateTextField;
@property(nonatomic,retain) IBOutlet UITextField *addressTextField;
@property(nonatomic,retain) IBOutlet UITextField *zipTextField;
@property(nonatomic,retain) IBOutlet UITextField *emailTextField;
@property NSString *name;
@property NSString *surname;
@property NSString *bornDate;
@property NSString *address;
@property NSString *zipCode;
@property NSString *miaEmail;
- (IBAction)checkData:(id)sender;
- (IBAction)backgroundTap:(id)sender;
- (IBAction)doneButtonPressed:(id)sender;
- (void)sendMail;
@end
实现文件是
//
// Reclutamento.m
// Pompieri
//
// Created by Reda Bousbah on 15.01.13.
// Copyright (c) 2013 Reda Bousbah. All rights reserved.
//
#import "Reclutamento.h"
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface Reclutamento ()
@end
@implementation Reclutamento
@synthesize nameTextField, surnameTextField, bornDateTextField, addressTextField, zipTextField, emailTextField;
@synthesize name, surname, bornDate, address, zipCode, miaEmail;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
nameTextField.text = nil;
surnameTextField.text = nil;
bornDateTextField.text = nil;
addressTextField.text = nil;
zipTextField.text = nil;
emailTextField.text = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
#pragma - getting info from the UI
//NSString *test = nil;
- (IBAction)checkData:(id)sender
{
/*
name = nameTextField.text;
surname = surnameTextField.text;
bornDate = bornDateTextField.text;
address = addressTextField.text;
zipCode = zipTextField.text;
email = emailTextField.text;
*/
//NSLog(@" Nome: %@ \n Cognome: %@ \n Data di nascita: %@ \n Indirizzo: %@ \n Zip: %@ \n email: %@ \n", name, surname, bornDate, address, zipCode, email);
unsigned int x,a = 0;
NSMutableString *messaggioErrore; //stringa variabile
messaggioErrore = [NSMutableString stringWithFormat: @""]; //le stringhe mutabili vanno inizializzate in questo modo!
for (x=0; x<6; x++)
{
switch (x) {
case 0:
if (nameTextField.text == nil) {
[messaggioErrore appendString:@"Nome, "];
a=1;
}
break;
case 1:
if (surnameTextField.text == nil)
{
[messaggioErrore appendString:@"Cognome, "];
a=1;
}
break;
case 2:
if (bornDateTextField.text == nil)
{
[messaggioErrore appendString:@"Data di nascita, "];
a=1;
}
break;
case 3:
if (addressTextField.text == nil)
{
[messaggioErrore appendString:@"Indirizzo, "];
a=1;
}
break;
case 4:
if (zipTextField.text == nil)
{
[messaggioErrore appendString:@"Zip Code, "];
a=1;
}
break;
case 5:
if (emailTextField.text == nil)
{
[messaggioErrore appendString:@"Email, "];
a=1;
}
break;
default:
break;
}
}
NSLog (@"Messaggio errore: %@", messaggioErrore);
if (a == 1) {
NSMutableString *popupErrore;
popupErrore = [NSMutableString stringWithFormat: @"Per inviare compilare i seguenti campi: "];
[popupErrore appendString:messaggioErrore]; //aggiungo i miei errori
[popupErrore appendString: @" grazie della comprensione."]; //
NSLog(@"%@", popupErrore);
UIAlertView *chiamataEffettuata = [[UIAlertView alloc]
initWithTitle:@"ATTENZIONE" //titolo del mio foglio
message:popupErrore
delegate:self
cancelButtonTitle:@"Ok, correggo" //bottone con cui si chiude il messaggio
otherButtonTitles:nil, nil];
[chiamataEffettuata show]; //istanza per mostrare effettivamente il messaggio
}
else
{
name = nameTextField.text;
surname = surnameTextField.text;
bornDate = bornDateTextField.text;
address = addressTextField.text;
zipCode = zipTextField.text;
email = emailTextField.text;
NSMutableString *datiUtente;
datiUtente = [NSMutableString stringWithFormat: @"<br><br> <b>Nome:</b> %@ <br> <b>Cognome:</b> %@ <br> <b>Data di nascita:</b> %@ <br> <b>Indirizzo:</b> %@ <br> <b>Zip:</b> %@ <br> <b>Email:</b> %@ <br>", name, surname, bornDate, address, zipCode, miaEmail];
NSLog(@"Dati utente: %@", datiUtente);
NSMutableString *testoMail;
testoMail = [NSMutableString stringWithFormat: @"Salve, questa mail è stata generata automaticamente dalla applicazione per iPhone ''<b>Pompieri Locarno</b>'' <br> i dati relativi all'utente sono: %@", datiUtente];
NSLog(@"%@", testoMail);
//MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Reclutamento pompieri"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d3422383d223f230d2a202c2421632e2220" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
//NSArray *ccRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b687e7874757f5b7e637a766b777e35787476" rel="noreferrer noopener nofollow">[email protected]</a>",@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7b3afaeb5a387a2bfa6aab7aba2e9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a>", nil];
//NSArray *bccRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="066069737446637e676b766a632865696b" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
// Attach an image to the email.
//NSString *path = [[NSBundle mainBundle] pathForResource:@"ipodnano" ofType:@"png"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"ipodnano"];
// Fill out the email body text.
//NSMutableString *emailBody;
testoMail = [NSMutableString stringWithFormat: @"%@", testoMail];
[picker setMessageBody:testoMail isHTML:YES]; //HTML!!!!!!
// Present the mail composition interface.
[self presentViewController:picker animated:YES completion:nil];
// The mail compose view controller delegate method
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES];
}
}
}
#pragma mark - Mandare email
/*
- (void)sendMail:(NSMutableString*)testoMail{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Reclutamento pompieri"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c4e59585d125e53494f5e5d547c5b515d5550125f5351" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
//NSArray *ccRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0675636569686246637e676b766a632865696b" rel="noreferrer noopener nofollow">[email protected]</a>",@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02766a6b706642677a636f726e672c616d6f" rel="noreferrer noopener nofollow">[email protected]</a>", nil];
//NSArray *bccRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd9b92888fbd98859c908d9198d39e9290" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
// Attach an image to the email.
//NSString *path = [[NSBundle mainBundle] pathForResource:@"ipodnano" ofType:@"png"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"ipodnano"];
// Fill out the email body text.
NSString *emailBody = @"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
// Present the mail composition interface.
[self presentViewController:picker animated:YES completion:nil];
}
*/
#pragma mark - methods to control the keyboard
- (IBAction)backgroundTap:(id)sender //method for resign the keyboard when the background is tapped
{
[nameTextField resignFirstResponder];
[surnameTextField resignFirstResponder];
[bornDateTextField resignFirstResponder];
[addressTextField resignFirstResponder];
[zipTextField resignFirstResponder];
[emailTextField resignFirstResponder];
}
- (IBAction)doneButtonPressed:(id)sender
{
NSLog( @"done button pressed");
[sender resignFirstResponder];
}
@end
最佳答案
您似乎已经在方法中定义了 - (void)mailComposeController:(MFMailComposeViewController *)controller
。
请在方法外部定义它。
编辑
正如我所说,将定义移出方法。 这是完整正确的 .m 文件
//
// Reclutamento.m
// Pompieri
//
// Created by Reda Bousbah on 15.01.13.
// Copyright (c) 2013 Reda Bousbah. All rights reserved.
//
#import "Reclutamento.h"
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface Reclutamento ()
@end
@implementation Reclutamento
@synthesize nameTextField, surnameTextField, bornDateTextField, addressTextField, zipTextField, emailTextField;
@synthesize name, surname, bornDate, address, zipCode, miaEmail;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
nameTextField.text = nil;
surnameTextField.text = nil;
bornDateTextField.text = nil;
addressTextField.text = nil;
zipTextField.text = nil;
emailTextField.text = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
#pragma - getting info from the UI
//NSString *test = nil;
- (IBAction)checkData:(id)sender
{
/*
name = nameTextField.text;
surname = surnameTextField.text;
bornDate = bornDateTextField.text;
address = addressTextField.text;
zipCode = zipTextField.text;
email = emailTextField.text;
*/
//NSLog(@" Nome: %@ \n Cognome: %@ \n Data di nascita: %@ \n Indirizzo: %@ \n Zip: %@ \n email: %@ \n", name, surname, bornDate, address, zipCode, email);
unsigned int x,a = 0;
NSMutableString *messaggioErrore; //stringa variabile
messaggioErrore = [NSMutableString stringWithFormat: @""]; //le stringhe mutabili vanno inizializzate in questo modo!
for (x=0; x<6; x++)
{
switch (x) {
case 0:
if (nameTextField.text == nil) {
[messaggioErrore appendString:@"Nome, "];
a=1;
}
break;
case 1:
if (surnameTextField.text == nil)
{
[messaggioErrore appendString:@"Cognome, "];
a=1;
}
break;
case 2:
if (bornDateTextField.text == nil)
{
[messaggioErrore appendString:@"Data di nascita, "];
a=1;
}
break;
case 3:
if (addressTextField.text == nil)
{
[messaggioErrore appendString:@"Indirizzo, "];
a=1;
}
break;
case 4:
if (zipTextField.text == nil)
{
[messaggioErrore appendString:@"Zip Code, "];
a=1;
}
break;
case 5:
if (emailTextField.text == nil)
{
[messaggioErrore appendString:@"Email, "];
a=1;
}
break;
default:
break;
}
}
NSLog (@"Messaggio errore: %@", messaggioErrore);
if (a == 1) {
NSMutableString *popupErrore;
popupErrore = [NSMutableString stringWithFormat: @"Per inviare compilare i seguenti campi: "];
[popupErrore appendString:messaggioErrore]; //aggiungo i miei errori
[popupErrore appendString: @" grazie della comprensione."]; //
NSLog(@"%@", popupErrore);
UIAlertView *chiamataEffettuata = [[UIAlertView alloc]
initWithTitle:@"ATTENZIONE" //titolo del mio foglio
message:popupErrore
delegate:self
cancelButtonTitle:@"Ok, correggo" //bottone con cui si chiude il messaggio
otherButtonTitles:nil, nil];
[chiamataEffettuata show]; //istanza per mostrare effettivamente il messaggio
}
else
{
name = nameTextField.text;
surname = surnameTextField.text;
bornDate = bornDateTextField.text;
address = addressTextField.text;
zipCode = zipTextField.text;
email = emailTextField.text;
NSMutableString *datiUtente;
datiUtente = [NSMutableString stringWithFormat: @"<br><br> <b>Nome:</b> %@ <br> <b>Cognome:</b> %@ <br> <b>Data di nascita:</b> %@ <br> <b>Indirizzo:</b> %@ <br> <b>Zip:</b> %@ <br> <b>Email:</b> %@ <br>", name, surname, bornDate, address, zipCode, miaEmail];
NSLog(@"Dati utente: %@", datiUtente);
NSMutableString *testoMail;
testoMail = [NSMutableString stringWithFormat: @"Salve, questa mail è stata generata automaticamente dalla applicazione per iPhone ''<b>Pompieri Locarno</b>'' <br> i dati relativi all'utente sono: %@", datiUtente];
NSLog(@"%@", testoMail);
//MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Reclutamento pompieri"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4bdabb1b4abb6aa84a3a9a5ada8eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
//NSArray *ccRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96e5f3f5f9f8f2d6f3eef7fbe6faf3b8f5f9fb" rel="noreferrer noopener nofollow">[email protected]</a>",@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3d7cbcad1c7e3c6dbc2ced3cfc68dc0ccce" rel="noreferrer noopener nofollow">[email protected]</a>", nil];
//NSArray *bccRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2b22383f0d28352c203d2128632e2220" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
// Attach an image to the email.
//NSString *path = [[NSBundle mainBundle] pathForResource:@"ipodnano" ofType:@"png"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"ipodnano"];
// Fill out the email body text.
//NSMutableString *emailBody;
testoMail = [NSMutableString stringWithFormat: @"%@", testoMail];
[picker setMessageBody:testoMail isHTML:YES]; //HTML!!!!!!
// Present the mail composition interface.
[self presentViewController:picker animated:YES completion:nil];
}
}
// The mail compose view controller delegate method
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark - Mandare email
/*
- (void)sendMail:(NSMutableString*)testoMail{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Reclutamento pompieri"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b293e3f3a7539342e28393a331b3c363a323775383436" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
//NSArray *ccRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdbea8aea2a3a98da8b5aca0bda1a8e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>",@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c6874756e785c79647d716c7079327f7371" rel="noreferrer noopener nofollow">[email protected]</a>", nil];
//NSArray *bccRecipients = [NSArray arrayWithObjects:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bede4fef9cbeef3eae6fbe7eea5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a>",nil];
[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
// Attach an image to the email.
//NSString *path = [[NSBundle mainBundle] pathForResource:@"ipodnano" ofType:@"png"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"ipodnano"];
// Fill out the email body text.
NSString *emailBody = @"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
// Present the mail composition interface.
[self presentViewController:picker animated:YES completion:nil];
}
*/
#pragma mark - methods to control the keyboard
- (IBAction)backgroundTap:(id)sender //method for resign the keyboard when the background is tapped
{
[nameTextField resignFirstResponder];
[surnameTextField resignFirstResponder];
[bornDateTextField resignFirstResponder];
[addressTextField resignFirstResponder];
[zipTextField resignFirstResponder];
[emailTextField resignFirstResponder];
}
- (IBAction)doneButtonPressed:(id)sender
{
NSLog( @"done button pressed");
[sender resignFirstResponder];
}
@end
关于objective-c - 邮件编辑器在 Xcode 中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14400184/