I made a basic app that for now just has a button that you click, and it brings up the sms composer (ios7 and xcode 5). I think I've handled everything well. The simulator doesn't support sending messages, so I tried on my phone, but the message never actually sends. You can click the send button and cancel button fine, but again, the message never sends. Any ideas? Here is my code:
- (IBAction)text:(UIButton *)sender {
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
[messageVC setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
NSString *smsString = [NSString stringWithFormat:#"message to send"];
messageVC.body = smsString;
messageVC.recipients = #[#"number to send to..."];
messageVC.messageComposeDelegate = self;
[self presentViewController:messageVC animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
NSLog(#"Message was cancelled");
[self dismissViewControllerAnimated:YES completion:NULL]; break;
case MessageComposeResultFailed:
NSLog(#"Message failed");
[self dismissViewControllerAnimated:YES completion:NULL]; break;
case MessageComposeResultSent:
NSLog(#"Message was sent");
[self dismissViewControllerAnimated:YES completion:NULL]; break;
default:
break;
}
}
EDIT: So I tried it this morning (I did nothing to it overnight) and it worked. Not sure what the issue was. Thanks though!
I assume the messageVC.recipients is in correct format (array of numbers)?
Try:
//check if the device can send text messages
if(![MFMessageComposeViewController canSendText]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device cannot send text messages" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
//set receipients
NSArray *recipients = [NSArray arrayWithObjects:#"0912345679",#"0923456790",#"0934567901", nil];
//set message text
NSString * message = #"this is a test sms message.";
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipients];
[messageController setBody:message];
// Present message view controller on screen
[self presentViewController:messageController animated:YES completion:nil];
Try This In Your MessageCompose Delegate:
(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Message was cancelled");
break;
case MessageComposeResultFailed:
NSLog(#"Message failed");
break;
case MessageComposeResultSent:
NSLog(#"Message was sent");
break;
default:
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
Related
I'm not able to call Mail composer delegate method even if I'm writing below code:
-(void)openMailComposerInViewController:(UIViewController *)hostController{
if ([MFMailComposeViewController canSendMail])
{
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"Test Email";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"support#test.com"];
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:emailTitle];
[mailComposer setMessageBody:messageBody isHTML:NO];
[mailComposer setToRecipients:toRecipents];
// Present mail view controller on screen
[hostController presentViewController:mailComposer animated:YES completion:NULL];
mailController = hostController;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
pragma mark - Mail
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[mailController dismissViewControllerAnimated:YES completion:NULL];
}
I do not want to call mail composer on self
However, my delegate is not getting called. Please help. The application crashed on clicking on Cancel or Send mail button
I experienced this problem, and my problem was I had the following code:
self.mailComposer.mailComposeDelegate = self
self.mailComposer = MFMailComposeViewController()
I was setting the delegate before I initialized the mailComposer object. I just switched those 2 lines to this and it worked:
self.mailComposer = MFMailComposeViewController()
self.mailComposer.mailComposeDelegate = self
Because of ARC, I was losing the viewcontroller in which the delegate was written so it was not gettting called. Below code helped me fix the issue:
+(MailComposer *)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMailComposer = [[MailComposer alloc]init];
});
return sharedMailComposer;
}
Then I used the above instance while calling the MailComposerViewController
[[MailComposer sharedInstance] openMailComposerInViewController:[(UINavigationController *)moduleRouter.container topViewController]];
It Worked :)
your class instance must be same for setMailComposeDelegate and presentViewController.
mailComposer.mailComposeDelegate = hostController;
[hostController presentViewController:mailComposer animated:YES completion:nil];
I am facing one issue for send message to multiple contacts :
301 up contacts : Message app is not opening, If we click invite on multiple time then iMessage is opening but it will take more time (2 minute) to load message
351 up contacts : Message is open with black new message screen then come back to our application with out opening message screen.
Here is my code:
contacts is the array of phone number
NSMutableArray *contacts = [NSMutableArray array];
for (User *user in users) {
if (user.phone.length) {
NSString *strphonenumber = [NSString stringWithFormat:#"%#",user.phone];
[contacts addObject:strphonenumber];
}
}
MFMessageComposeViewController *messanger = [[MFMessageComposeViewController alloc]init];
messanger.messageComposeDelegate = self;
messanger.recipients = contacts;
messanger.body = [NSString stringWithFormat:#“body”;
[self presentViewController:messanger animated:YES completion:NULL];
I am getting this error:
<CKSMSComposeRemoteViewController: 0x12802f810> timed out waiting for fence barrier from com.apple.mobilesms.compose
Try this...
NSString *strContacts = [NSString stringWithFormat:#"%#",add multipal contacts];
MFMessageComposeViewController *message = [[MFMessageComposeViewController new];
message.recipients = #[strContacts];
I to had the same problem then figured
messanger.recipients = // should always be an array of strings.
Make sure the phone numbers you send to messanger.recipients are NSString.
this works for me:
set delegate to interface:
#interface ViewController <MFMessageComposeViewControllerDelegate>{}
check if device is able to send message
if([MFMessageComposeViewController canSendText] ){
//device is possible to send messages
}else{
//device can't send messages
}
prepare message:
MFMessageComposeViewController* comp = [[MFMessageComposeViewController alloc] init];
//set properties
comp.body = #"body";
comp.recipients = [NSArray arrayWithObjects:phone1, phone2, nil];
comp.messageComposeDelegate = self;
open dialog:
[self presentViewController:comp animated:YES completion:nil];
determine result
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
//test result
switch (result) {
case MessageComposeResultCancelled:
[self makeAlert:#"Result canceled"];
break;
//message was sent
case MessageComposeResultSent:
[self makeAlert:#"Result sent"];
break;
case MessageComposeResultFailed:
[self makeAlert:#"Result Failed"];
break;
default:
break;
}
//dismiss view
[self dismissViewControllerAnimated:YES completion:nil];
}
Just use this method:
- (IBAction)sendSMS:(id)sender {
MFMessageComposeViewController *controller =
[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText]){
controller.body = #"Hello";
controller.recipients = [NSArray arrayWithObjects:#"12345678",#"87654321", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
Callback method:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
switch (result) {
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(#"Error occured");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I'm using cocos2d-x framework and I need to popup the mail when clicking button.
The code works fine on iphone5 (6.0), ipod touch 5(6.0):
MailSender.h
#interface MailSender : UIViewController <MFMailComposeViewControllerDelegate>
{
UIViewController *currentModalViewController;
}
-(void)sendMail:(const char *)subject receiver:(const char *)receiver;
#end
MailSender.mm
#import "MailSender.h"
#import "../cocos2dx/platform/ios/EAGLView.h"
#implementation MailSender
- (void)sendMail:(const char *)subject receiver:(const char *)receiver
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithUTF8String:subject]];
NSArray *toRecipients = [NSArray arrayWithObject:[NSString stringWithFormat:#"%s", receiver]];
[mailer setToRecipients: toRecipients];
//NSString *emailBody = [NSString stringWithFormat:#"<p>This is a sample posting in iOS. My Score is %s!</p>",score];
NSString *emailBody = #"";
[mailer setMessageBody:emailBody isHTML:YES];
// only for iPad
// mailer.modalPresentationStyle = UIModalPresentationFormSheet;
UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
currentModalViewController = [UIViewController alloc];
[rootViewController.view addSubview:currentModalViewController.view];
[currentModalViewController presentViewController:mailer animated:true completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
const char *message;
switch (result)
{
case MFMailComposeResultCancelled:
message = "Mail cancelled";
break;
case MFMailComposeResultSaved:
message = "Mail saved";
break;
case MFMailComposeResultSent:
message = "Mail send";
break;
case MFMailComposeResultFailed:
message = "Mail failed";
break;
default:
message = "Mail cancelled";
break;
}
NSLog(#"%s",message);
[currentModalViewController dismissViewControllerAnimated:true completion:nil];
[currentModalViewController.view.superview removeFromSuperview];
[currentModalViewController release];
}
#end
But on my ipad mini (6.0) the mail popped up correctly but when clicked the "send mail" or "cancel" the view was removed and leaving a black screen (everything on the screen is gone)
Any advice will be appreciated, thanks :)
try this code
if ([MFMailComposeViewController canSendMail]) {
mailComposer = [[MFMailComposeViewController alloc]init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:#[#"yourmail#com"]];
[mailComposer setSubject:#"Subject"];
[mailComposer setMessageBody:#"hello \n" isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
}
else{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Error"
message:#"can not send mail with this device"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
}
pragma mark MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (result) {
NSLog(#"Result : %d",result);
}
if (error) {
NSLog(#"Error : %#",error);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I'm using this code for feedback via email in my cocos2d-x game.
Application::getInstance()->openURL("mailto:" + SUPPORT_EMAIL);
You can add subject:
Application::getInstance()->openURL("mailto:" + SUPPORT_EMAIL + "?subject=Hello from NX");
This solution tested on iOS and Android.
Hi I am using MFMessageComposeViewController for messaging in an iPhone app.
As this is an iPhone app it also supports iPod. And when clicking on the message button the app crashes as messaging is not available on iPod. So is there a way to check whether the device is an iPod so that i can hide the message button so that the user may not click on message in iPod and crash.
This is the code I have used for messaging.
- (IBAction)Message:(id)sender
{
MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
messaging.messageComposeDelegate=self;
[messaging setBody:#"Will of the People""\n""http://bit.ly/1gZhZye"];
[self presentViewController:messaging animated:YES completion:nil];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:^{UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Done" message:nil delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
}];
}
And this seems to be working fine in iPhone. I need a way to disable this button when the user is using iPod.
You can use the canSendText class method for doing this:
- (IBAction)Message:(id)sender
{
if ([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
messaging.messageComposeDelegate=self;
[messaging setBody:#"Will of the People""\n""http://bit.ly/1gZhZye"];
[self presentViewController:messaging animated:YES completion:nil];
}
}
Reference :
canSendText
Returns a Boolean value indicating whether the current device is
capable of sending text messages.
+ (BOOL)canSendText
Return Value
YES if the device can send text messages or NO if it cannot.
Discussion
Always call this method before attempting to present the message
compose view controller. A device may be unable to send messages if it
does not support messaging or if it is not currently configured to
send messages. This method applies only to the ability to send text
messages via iMessage, SMS, and MMS.
To be notified of changes in the availability of sending text
messages, register as an observer of the
MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification
notification. Availability
Available in iOS 4.0 and later.
Declared In MFMessageComposeViewController.h
There's a method on MFMEssageComposeViewController:
if ([MFMessageComposeViewController canSendText]) {
}
else {
NSLog(#"Cannot send text");
}
NSString *deviceType = [UIDevice currentDevice].model;
if ([deviceType hasPrefix:#"iPod"])
{
//It's iPod;
//Disable button
}
First detect device by using following way and if device does not support messenger than show alert.
Form here you can get more idea about different device detection :
Determine device (iPhone, iPod Touch) with iPhone SDK
- (IBAction)Message:(id)sender
{
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:#"iPod Touch 5G"]) {
// here your alert view to show msg
} else {
if ([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
messaging.messageComposeDelegate=self;
[messaging setBody:#"Will of the People""\n""http://bit.ly/1gZhZye"];
[self presentViewController:messaging animated:YES completion:nil];
}
}
}
You can use the UIDevice class to check the device type
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType hasPrefix:#"iPod"])
// it's an iPod
or you can use [MFMessageComposeViewController canSendText] to check the message can be send from the device or not
-(IBAction)btnByEmailPressed:(id)sender
{
if ([MFMailComposeViewController canSendMail] == NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"This device is not able to send mail or Email account is not configured." delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
return;
}
else
{
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setTitle:#"Invitation"];
[controller setSubject:#"My Subject"];
[controller setMessageBody:#"Your Text" isHTML:YES];
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self becomeFirstResponder];
NSString *strMailResult;
switch (result)
{
case MFMailComposeResultCancelled:
strMailResult = NSLocalizedString(#"E-Mail Cancelled",#"");
break;
case MFMailComposeResultSaved:
strMailResult = NSLocalizedString(#"E-Mail Saved",#"");
break;
case MFMailComposeResultSent:
strMailResult = NSLocalizedString(#"E-Mail Sent",#"");
break;
case MFMailComposeResultFailed:
strMailResult = NSLocalizedString(#"E-Mail Failed",#"");
break;
default:
strMailResult = NSLocalizedString(#"E-Mail Not Sent",#"");
break;
}
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"STEP-BY-STEP-STORY",#"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alertView show];
[self dismissViewControllerAnimated:YES completion:nil];
}
I have a TableView set to trigger a different ViewController when the user selects an item in the list, but I would like "Contact Us" to trigger the Messages Framework (In App Email) so the user can send an email and not get pushed to a ViewController.
Any suggestions? Below is a block of code that I'm using for "Contact Us".
Sorry if this is an obvious answer, learning as I go and envy all you wizards out there!
NSMutableDictionary *sectionContactUs = [NSMutableDictionary dictionary];
[sectionContactUs setObject:kSlideViewControllerSectionTitleNoTitle forKey:kSlideViewControllerSectionTitleKey];
[sectionContactUs setObject:#"Contact Us" forKey:kSlideViewControllerSectionTitleKey];
NSMutableDictionary *contactUsViewControllerDictionary = [NSMutableDictionary dictionary];
[contactUsViewControllerDictionary setObject:#"Contact Us" forKey:kSlideViewControllerViewControllerTitleKey];
[contactUsViewControllerDictionary setObject:#"ContactUsViewController" forKey:kSlideViewControllerViewControllerNibNameKey];
[contactUsViewControllerDictionary setObject:[ContactUsViewController class] forKey:kSlideViewControllerViewControllerClassKey];
[sectionTest setObject:[NSArray arrayWithObject:contactUsViewControllerDictionary] forKey:kSlideViewControllerSectionViewControllersKey];
[datasource addObject:sectionContactUs];
Use MFMailComposeComposeView:
First Import MessageUI framework in your project
And write #import <MessageUI/MessageUI.h> in your view controller also add MFMailComposeViewControllerDelegate at interface
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:#"test1#gmail.com", #"test2#gmail.com", nil];
[controller setToRecipients:toRecipients];
[controller setTitle:#"Contact Us"];
[controller setSubject:#"Your Mail Subject"];
[controller setMessageBody:#"Mail body here \n Comments" isHTML:NO];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
controller.modalPresentationStyle = UIModalPresentationFormSheet;
}
else
{
controller.modalPresentationStyle = UIModalPresentationFullScreen;
}
[self presentModalViewController:controller animated:YES];
Also add this methods
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
NSString *strMailResult;
switch (result)
{
case MFMailComposeResultCancelled:
strMailResult = NSLocalizedString(#"E-Mail Cancelled",#"");
break;
case MFMailComposeResultSaved:
strMailResult = NSLocalizedString(#"E-Mail Saved",#"");
break;
case MFMailComposeResultSent:
strMailResult = NSLocalizedString(#"E-Mail Sent",#"");
break;
case MFMailComposeResultFailed:
strMailResult = NSLocalizedString(#"E-Mail Failed",#"");
break;
default:
strMailResult = NSLocalizedString(#"E-Mail Not Sent",#"");
break;
}
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Message",#"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alertView show];
[self dismissModalViewControllerAnimated:YES];
}
you can open MFMailComposeViewController for send mail..
-(void)doEmail
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate =self;
[picker setMessageBody:#"Body message" isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
//////nslog (#"mail finished");
}