I am trying to send sms through device. But it crashes on the line
[self presentModalViewController:picker animated:YES];
CODE:
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
picker.recipients = rec; // your recipient number or self for testing
picker.body = billInfo;
[self presentModalViewController:picker animated:YES];
[picker release];
Error Log:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <billSummary: 0xfc7e780>.'
Have you checked +[MFMessageComposeViewController canSendText]? From the docs,
Before using this class, you must always check to see if the current device is configured to send SMS messages by calling the canSendText class method. If the user’s device is not set up for the delivery of SMS messages, you can notify the user or simply disable the SMS features in your application. You should not attempt to use this interface if the canSendText method returns NO.
Other reasons it might not work:
No SIM card?
Device isn't running iOS 4.
Device is an iPod Touch/iPad.
"Device" is actually the simulator.
[self presentModalViewController:picker.view animated:YES];
<MFMessageComposeViewControllerDelegate>
should be added in the .h of your class.
MFMessageComposeViewController is the subclass of UINavatigationController.
so apply pushViewController instead of presentmodalviewController
Related
Currently i am working in iPhone application, Using MFMessageComposeViewController to develop this application and its working fine.
But i want, message compose screen don't show on the screen, then the Message send programmatically ,
How to do this? it is possible? please help me
I tried this:
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Hi";
controller.recipients = [NSArray arrayWithObjects:#"12345678", #"87654321", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
I've tried to do this once but but it wont be accepted by app store. Apple wont let you send message/email without user knowledge about it.
You can use a sms:[target phone number] URL to open the SMS application, but there are no indications on how to prefill a SMS body with text (see this post on Apple Developer Forums).
You can't use the Message Framework to send a message without the user knowing about it. One common solution is to send the message details to a web service running on your server and send the message from the server. As long as you're not trying to be sneaky, I don't think there's anything wrong with doing that.
I'd like to setup a button in the iPhone app I'm making to send an email with the results of some operations performed earlier in the app. I've read other people comment on here that you can use other frameworks but, frankly, I can't find one that actually works. I've heard MailCore is good. Has anyone been able to send an email seamlessly through an iPhone app?
Note: I do not want to exit the app to send the email.
Answer From this Question: How can I send mail from an iPhone application
On iPhone OS 3.0 and later you should use the MFMailComposeViewController class, and the MFMailComposeViewControllerDelegate protocol, that that tucked away in the MessageUI framework. Note you must link the Message UI Framework and import its headers.
#import <MessageUI/MFMailComposeViewController.h>
First to send a message:
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"My Subject"];
[controller setMessageBody:#"Hello there." isHTML:NO];
if (controller) [self presentModalViewController:controller animated:YES];
[controller release];
Then the user does the work and you get the delegate callback in time:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;{
if (result == MFMailComposeResultSent) {
NSLog(#"It's away!");
}
[self dismissModalViewControllerAnimated:YES];
}
In this case, your best bet is to use a service like Amazon Simple Email Service (SES). It will enable you to send the email without ever leaving the application (unlike the other solutions listed). Amazon AWS has an iOS SDK which can utilize the SES service. This service is super simple to use, and the cost is extremely low.
http://aws.amazon.com/sdkforios/
MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
NSString *emailBody = #"Write email body text here........";
[mailComposer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
I am using the following code within my application to create and populate a SMS for the user to send. This code is called by pressing a UIButton.
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Hello from Mugunth";
controller.recipients = [NSArray arrayWithObjects:#"12345678", #"87654321", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
On the initial loading of the view, I would like to test the device capabilities, and hide the button if necessary (for example on an iPod touch).
Does anyone have any code examples on how to do this? Please note, I am only targeting iOS 4.0 and greater, I am aware the above code will not work on devices using earlier versions of iOS.
Regards
[MFMessageComposeViewController canSendText] will determine whether the device you are on can send text messages. I've known it to react correctly on an iPhone with SIM, an iPod Touch and the iOS Simulator. I've not tested it on e.g. an iPhone without SIM or an iPad.
You can use the below code to detect whether the device can send SMS or not:
Preferred:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"sms:"]]) {
}
or
[MFMessageComposeViewController canSendText]
Make sure you call [MFMessageComposeViewController canSendText] prior to allocating an instance of MFMessageComposeViewController. Newer versions of iOS will throw an alert informing the user that they can't send messages as soon as you instantiate MFMessageComposeViewController.
I wrote a code that utilizes the MFMAilComposibleViewController to harness in-app emails. I specified the delegate as so under, but my delegate method isn't getting called. This is the first time I'm getting this type of error; I normally develop under the iPhone but for a client I need to develop under an iPad.
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setModalPresentationStyle: UIModalPresentationFormSheet];
[mailComposer setSubject: #"Hello World"];
[mailComposer setMailComposeDelegate: self];
[mailComposer setMessageBody: #"<font color='red'>Hello</font> <font color='blue'>World</font>" isHTML:YES];
[self presentModalViewController: mailComposer animated: YES];
This snippet is an exact copy-paste of the method that calls the mailview. Please note, I did specify in my header the MFMailViewControllerDelegate protocol.
Thank You!
Have you tested this in simulator? As i've experienced before, the mail composer doesn't work in the simulator but in the device. Try it.
HI,
I am new to iphone development.I have created the tabbar programmatically and sets five views in the tabbar. Now i want to load an email application view when i clicked the tabbar.This works properly.When i clicked the next tabbar and come back to the email view, i am able to see the normal view and not the Email view.Only one time i am able to see my mail application.I have mail application in the viewDidLoad method. So please guide me.
Here is my code,
- (void)viewDidLoad {
[super viewDidLoad];
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mail setToRecipients:[NSArray arrayWithObjects:#"aaa#gmail.com",nil]];
[mail setSubject:#"Title"];
[self presentModalViewController:mail animated:NO];
}
[mail release];
}
Thanks.
viewDidLoad only runs after the nib file has been loaded, which is once the first time the viewController is shown and then once after any memory warnings are sent.
You want to use viewDidAppear: instead which is called every time after the viewController comes into view.
If you use viewDidAppear method it will be keep on calling the mail view.So use viewWillAppear method.