简体   繁体   English

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

[英]Email error (undeclared identifier)

How would I fix this code so that the - (void)mailComposeController:(MFMailComposeViewController will not be considered an error in Xcode. 我将如何解决此代码,以便-(void)mailComposeController:((MFMailComposeViewController)在Xcode中不会被视为错误。

My .h file 我的.h文件

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

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


@end

Here is my .m file 这是我的.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

This is going to send a .xls or .CSV file created inapt when working hopefully. 当希望工作时,这将发送一个创建为inapt的.xls或.CSV文件。

Thanks in advance. 提前致谢。

You aren't giving us much to work off of (a specific error message would be nice). 您没有付出太多努力(特定的错误消息会很不错)。 Based on what you've given us, though, I believe you need to include the MessageUI framework, and add this line to the top of your file: 不过,根据您给我们的介绍,我相信您需要包括MessageUI框架,并将此行添加到文件顶部:

#import <MessageUI/MFMailComposeViewController.h>

I this answer for my assumption for you. 我这个答案是我对你的假设。 because you are not provide much more details about your error 因为您没有提供有关错误的更多详细信息

@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 mailComposeDelegate

The mail composition view controller's delegate. 邮件组成视图控制器的委托。

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

Discussion 讨论区

The delegate object is responsible for dismissing the view presented by this view controller at the appropriate time. 委托对象负责在适当的时间关闭此视图控制器提供的视图。 Therefore, you should always provide a delegate and that object should implement the methods of the MFMailComposeViewControllerDelegate protocol. 因此,您应始终提供一个委托,并且该对象应实现MFMailComposeViewControllerDelegate协议的方法。

sample apple code 示例苹果代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM