电子邮件错误(未申报标识符)



我将如何修复此代码,以便 - (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文件。

预先感谢。

您没有给我们太多工作(特定的错误消息将很好)。但是,根据您给我们的内容,我相信您需要包括MessageUI框架,然后将此行添加到文件顶部:

#import <MessageUI/MFMailComposeViewController.h>

i这个答案是为您的假设。因为您没有提供有关错误的更多详细信息

@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

更多详细信息

mailcomposedelegate

邮件组成视图控制器的代表。

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

讨论

代表对象负责在适当时间驳回此视图控制器提出的观点。因此,您应始终提供一个委托,该对象应实现MFMailComposeViewControllerDelegate协议的方法。

示例苹果代码

最新更新