电子邮件错误(未声明的标识符)

时间:2014-02-02 03:57:29

标签: ios mfmailcomposeviewcontroller

我如何修复此代码,以便 - (void)mailComposeController :( MFMailComposeViewController在Xcode中不会被视为错误。

我的.h文件

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface EmailViewController : UIViewController <MFMailComposeViewControllerDelegate>
- (IBAction)SendIt:(id)sender;


@end

这是我的.m文件

#import "EmailViewController.h"

@interface EmailViewController ()

@end

@implementation EmailViewController

- (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.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)SendIt:(id)sender {
    if (![MFMailComposeViewController canSendMail]) {
        //Show alert that device cannot send email, this is because an email account hasn't been setup.
    }

    else {

        //**EDIT HERE**
        //Use this to retrieve your recently saved file

        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filename = [documentPath stringByAppendingPathComponent:@"Cartier.xls"];

        //**END OF EDIT**

        NSString *mimeType = @"application/vnd.ms-excel"; //This should be the MIME type for els files. May want to double check.
        NSData *fileData = [NSData dataWithContentsOfFile:filename];
        NSString *fileNameWithExtension = @"Cartier.xls"; //This is what you want the file to be called on the email along with it's extension:

        //If you want to then delete the file:
        NSError *error;
        if (![[NSFileManager defaultManager] removeItemAtPath:filename error:&error])
            NSLog(@"ERROR REMOVING FILE: %@", [error localizedDescription]);


        //Send email
        MFMailComposeViewController *mailMessage = [[MFMailComposeViewController alloc] init];
        [mailMessage setMailComposeDelegate:self];
        [mailMessage addAttachmentData:fileData mimeType:mimeType fileName:fileNameWithExtension];
        [self presentViewController:mailMessage animated:YES completion:nil];
    }


    //error is line below
    **- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error** {

        switch (result)
        {
            case MFMailComposeResultCancelled:
                NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
                break;
            case MFMailComposeResultSaved:
                NSLog(@"Mail saved: you saved the email message in the drafts folder.");
                break;
            case MFMailComposeResultSent:
                NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
                break;
            case MFMailComposeResultFailed:
                NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
                break;
            default:
                NSLog(@"Mail not sent.");
                break;
        }

        [controller dismissViewControllerAnimated:YES completion:nil];
    }

}
@end

这将在希望工作时发送一个.xls或.CSV文件。

提前致谢。

2 个答案:

答案 0 :(得分:1)

你没有给我们太多的工作(特定的错误信息会很好)。但是,根据您提供的内容,我认为您需要包含MessageUI框架,并将此行添加到文件的顶部:

#import <MessageUI/MFMailComposeViewController.h>

答案 1 :(得分:1)

我回答了我对你的假设。因为您没有提供有关错误的更多详细信息

@interface AppViewController : UIViewController <MFMailComposeViewControllerDelegate> { }



<strong> picker.mailComposeDelegate = self;</strong> // &lt;- very important step if you want feedbacks on what the user did with your email sheet

more details

<强> mailComposeDelegate

邮件撰写视图控制器的委托。

@property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;

讨论

委托对象负责在适当的时间解除此视图控制器提供的视图。因此,您应始终提供委托,该对象应实现MFMailComposeViewControllerDelegate协议的方法。

sample apple code