문제

I have implemented in app Email and SMS sending functionality but i am not being able to send email and SMS. application is crashing

well . . . i am running it in Simulator but it should show the AlertView it's not supposed to crash

My Interface code is as Follows :-

 #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

MY implementation code is as Follows :-

#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];

}

when I click on sendEmail button the application crashes with this output

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) 

Please Tell me What Am i doing wrong ?

도움이 되었습니까?

해결책

I'm not quite sure whether this is the issue, but from your code, the line

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

which will just return an autoreleased copy of array, so just try

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

Since you have synthesized the property toRecipentsEmail, doing this will retain a copy of array.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top