Reputation: 1510
i am geeting crash using this code
Pushing a navigation controller is not supported
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipents];
[messageController setBody:message];
[self.parentViewController presentViewController:messageController animated:YES completion:nil];
Upvotes: 1
Views: 8069
Reputation: 2051
This isn't possible because the docs say that MFMessageComposeViewController
is a UINavigationController
O-C
@interface MFMessageComposeViewController : UINavigationController
swift
class MFMessageComposeViewController : UINavigationController
Upvotes: 0
Reputation: 4803
In case you are using storyboard
or .xib
, below code helps you.
Add MessageUI.framework
write this line for import class.
#import <MessageUI/MessageUI.h>
set delegate
@interface ListViewController : UIViewController <MFMessageComposeViewControllerDelegate, UINavigationControllerDelegate>
implement following method
- (IBAction)sendMessage:(UIButton *)sender
{
if([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.body = @"Hello...";
controller.recipients = [NSArray arrayWithObjects:@"recipient1, recipient2, recipient3", nil];
controller.messageComposeDelegate = self;
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
implement its delegate method for dismiss
#pragma mark - Message Delegate ============================
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled: break;
case MessageComposeResultFailed:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Sorry, something went wrong, please try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
break;
case MessageComposeResultSent: break;
default: break;
}
[[controller presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
Upvotes: 1
Reputation: 2163
[self presentViewController:messageController animated:YES completion:nil];
Upvotes: 4
Reputation: 3162
Is your View is in NavigationController Stack ? Try this-
[self.navigationController presentViewController:messageController animated:YES completion:nil];
Upvotes: 1