ios - MFMailComposerViewController 不发送电子邮件

标签 ios iphone objective-c mfmailcomposeviewcontroller mfmessagecomposeviewcontroller

我已经在应用程序中实现了电子邮件和短信发送功能 但我无法发送电子邮件和短信。应用程序崩溃

嗯。 。 。 我正在模拟器中运行它 但它应该显示 AlertView 它不应该崩溃

我的界面代码如下:-

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

@interface MessagingVC :UIViewController<MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate,UITextFieldDelegate,UITextViewDelegate>
- (IBAction)sendEmail:(id)sender;
- (IBAction)sendSMS:(id)sender;
@property (retain, nonatomic) IBOutlet UITextField *EmailToTxtField;
@property (retain, nonatomic) IBOutlet UITextField *PhoneToTxtField;
@property (retain, nonatomic) IBOutlet UITextView *massageBodyTxtView;
@property (retain, nonatomic) NSMutableArray *toRecipentsEmail;
@property (retain, nonatomic) NSMutableArray *toRecipentsPhone;
@property (retain, nonatomic) MFMessageComposeViewController *MessageCompVC;
@property (retain, nonatomic) MFMailComposeViewController *MailCompVC;
@end

我的实现代码如下:-

#import "MessagingVC.h"

@interface MessagingVC ()

@end

@implementation MessagingVC
@synthesize toRecipentsEmail,toRecipentsPhone,MailCompVC,MessageCompVC;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (BOOL)textView:(UITextView *)txtView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if( [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound ) {
        return YES;
    }

    [txtView resignFirstResponder];
    return NO;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{

    if (textField == _EmailToTxtField)
    {
        toRecipentsEmail = [NSMutableArray arrayWithArray:[textField.text componentsSeparatedByString:@","]];

    }
    if (textField == _PhoneToTxtField)
    {
        toRecipentsPhone = [NSMutableArray arrayWithArray:[textField.text componentsSeparatedByString:@","]];
    }
    [_EmailToTxtField resignFirstResponder];
    [_PhoneToTxtField resignFirstResponder];
    NSLog(@"toRecipentsEmail Count == %d",[toRecipentsEmail count]);
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _massageBodyTxtView.delegate = self;
    _EmailToTxtField.delegate = self;
    _PhoneToTxtField.delegate = self;
    toRecipentsPhone = [[NSMutableArray alloc] init];
    toRecipentsEmail = [[NSMutableArray alloc] init];

    // Do any additional setup after loading the view.
}

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

- (void)dealloc {

    [_EmailToTxtField release];
    [_PhoneToTxtField release];
    [_massageBodyTxtView release];
    [toRecipentsEmail release];
    [toRecipentsPhone release];
    [MailCompVC release];
    [MessageCompVC release];
    [super dealloc];
}
- (void)viewDidUnload {

    [self setEmailToTxtField:nil];
    [self setPhoneToTxtField:nil];
    [self setMassageBodyTxtView:nil];
    [self setToRecipentsEmail:nil];
    [self setToRecipentsPhone:nil];
    [self setMailCompVC:nil];
    [self setMessageCompVC:nil];
    [super viewDidUnload];
}
- (IBAction)sendEmail:(id)sender {


self.MailCompVC = [[MFMailComposeViewController alloc] init];
NSString *emailTitle = @"Subject";
MailCompVC.mailComposeDelegate = self;
[MailCompVC setToRecipients:toRecipentsEmail];
[MailCompVC setMessageBody:_massageBodyTxtView.text isHTML:NO];
[MailCompVC setSubject:emailTitle];

[self presentViewController:MailCompVC animated:YES completion:nil];


}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mail cancelled" message:@"Mail cancelled" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Mail saved" message:@"Mail saved" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert1 show];
        [alert1 release];
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Mail sent" message:@"Mail sent" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert2 show];
        [alert2 release];
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Mail sent failure" message:[NSString stringWithFormat:@"%@",[error localizedDescription]] delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert3 show];
        [alert3 release];
        break;
    default:
        break;
}

// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}

- (IBAction)sendSMS:(id)sender {

self.MessageCompVC = [[MFMessageComposeViewController alloc] init];
MessageCompVC.messageComposeDelegate = self;
[MessageCompVC setBody:_massageBodyTxtView.text];
[MessageCompVC setRecipients:toRecipentsPhone];
[self presentViewController:MessageCompVC animated:YES completion:nil];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
    case MessageComposeResultCancelled:
        NSLog(@"Message cancelled");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message cancelled" message:@"Message cancelled" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        break;
    case MessageComposeResultFailed:
        NSLog(@"Message Failed");
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message Failed" message:@"Message Failed" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert1 show];
        [alert1 release];
        break;
    case MessageComposeResultSent:
        NSLog(@"Message Sent");
        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Message Sent" message:@"Message Sent" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert2 show];
        [alert2 release];
        break;
    default:
        break;
}


[self dismissViewControllerAnimated:YES completion:nil];

}

当我点击发送电子邮件按钮时 应用程序崩溃并显示此输出

2013-10-31 12:57:14.095 MyApp[325:c07] toRecipentsEmail Count == 2
2013-10-31 12:57:21.952 MyApp[325:c07] -[__NSMallocBlock__ countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xf2ad5e0
2013-10-31 12:57:21.953 MyApp[325:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xf2ad5e0'
*** First throw call stack:
(0x1d65012 0x1a9ce7e 0x1df04bd 0x1d54bbc 0x1d5494e 0x346343 0x346523 0x59fde 0x1ab07055 0x9e42c0 0x9e4258 0xaa5021 0xaa557f 0xaa46e8 0xa13cef 0xa13f02 0x9f1d4a 0x9e3698 0x27d1df9 0x27d1ad0 0x1cdabf5 0x1cda962 0x1d0bbb6 0x1d0af44 0x1d0ae1b 0x27d07e3 0x27d0668 0x9e0ffc 0x2882 0x27b5)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

请告诉我我做错了什么?

最佳答案

我不太确定这是否是问题所在,但从您的代码来看,该行

toRecipentsEmail = [NSMutableArray arrayWithArray:[textField.text componentsSeparatedByString:@","]];

这只会返回一个自动释放的数组副本,所以试试

self.toRecipentsEmail = [NSMutableArray arrayWithArray:[textField.text componentsSeparatedByString:@","]] ;

由于您已经合成属性 toRecipientsEmail,这样做将保留数组的副本。

关于ios - MFMailComposerViewController 不发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19700998/

相关文章:

ios - 如何在 iOS 8 map 上同时显示当前位置和所有注释图钉

objective-c - 每个核心数据关系都必须有一个逆关系吗?

Objective-C 点语法或属性值?

iphone - NSURLConnection 挂了

ios - 快速创建自定义 TableViewCell

ios - iPhone App在模拟器上运行速度变慢

iphone - iOS 5 : Data encryption AES-256 EncryptWithKey: not found

javascript - Safari window.matchMedia 处理程序未调用

iphone - Objective-C 将十六进制字符串解析为整数

ios - 新闻API可以在应用程序中使用吗?