gpt4 book ai didi

ios - MFMailComposerViewController 不发送电子邮件

转载 作者:行者123 更新时间:2023-11-29 03:31:34 26 4
gpt4 key购买 nike

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

嗯。 。 。我正在模拟器中运行它但它应该显示 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com