如何使用6.2版在Xcode中发送电子邮件?

时间:2015-03-14 03:44:48

标签: ios objective-c email xcode6

我已经查看了使用Xcode在应用程序中发送电子邮件的各种教程,但我尝试过的大部分教程都不适用于当前版本的Xcode 6.2。当我运行IOS模拟器时,电子邮件视图会立即消失,或者我无法在任何字段中键入任何内容。我确保更新框架以使用MessageUI.framework。我还将发布我正在使用的当前代码。我想使用目标C作为语言,因为我创建的应用程序正在使用Objective C.有谁知道如何在当前版本(6.2)中实现电子邮件?

.h文件

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

@interface IntakeViewController : UIViewController <MFMailComposeViewControllerDelegate>{

}

- (IBAction)emailClicked:(id)sender;


@end

.m文件

#import "IntakeViewController.h"

@interface IntakeViewController ()

@end

@implementation IntakeViewController

- (IBAction)emailClicked:(id)sender {
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc]init];
        mail.mailComposeDelegate = self;
        [mail setSubject:@"Review"];
        NSArray *toRecipients = [NSArray arrayWithObjects:@"cbailey92@gmail.com", nil];
        [mail setToRecipients:toRecipients];
        NSString *body = @"You need more passion";
        [mail setMessageBody:body isHTML:NO];
        mail.modalPresentationStyle = UIModalPresentationPageSheet;
        [self presentViewController:mail animated:YES completion:nil];
    } else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Run Away!" message:@"You need more passion" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
    }

}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"Cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Saved");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Failed");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Sent");
            break;

        default:
            NSLog(@"Default");
            break;
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

0 个答案:

没有答案