How to send picture as an attachment with sms programmatically? - iphone

I want to send a picture via an iphone app.
Since it's not possible to send MMS, I found some answers speaking about sending the picture as an attachment with the SMS. How to do that programmatically?

Here is the code:
MFMessageComposeViewController *sms = [[MFMessageComposeViewController alloc]init];
[sms setMessageComposeDelegate:self];
[sms setRecipients:[NSArray arrayWithObject:#"123-4567"]];
[sms setSubject:#"Subject"];
[sms setBody:#"Body"];
[sms addAttachmentData:myData typeIdentifier:#"type" filename:myFile];
[sms setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:sms animated:YES completion:NO];
Hope it helps.

Related

MFMailComposersheet does not allocates in device iOS 5.1 version

I have the following line of code,
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:APP_NAME];
[picker addAttachmentData:pdfData mimeType:#"pdf" fileName:pdfFileName];
NSString *emailBody = #"";
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
But iPad just stuck at the first line of the code.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
When i put the break point on the another line it does not come to the second line of code it is just stuck and does not open MailCompserSheet and it is hung.
This problem appears only on iOS version 5.1 in iPad.
So anyway have idea what to do?
I guess you might not have configured mail in your iPad.
So before initializing the class MFMailComposeViewController, call MFMailComposeViewController's + (BOOL)canSendMail to check whether you can be able to send mail with the device or not.

Emailing contents of a UITextView

My app generates information from textfields to a uitextview, i was wondering how i could send this information on a email when the user clicks a button.
The mail application should open and it will have the contents of the uitextview in the body of the email.
Many Thanks
You have to do it with MFMailComposeViewController. Here's the code:
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
[composer setSubject:#"My Subject"];
[composer setMessageBody:#"Email Body" isHTML:YES];
[self presentModalViewController:composer animated:YES];
[composer release];
Just replace #"My Subject" with your real subject and #"Email Body" with your TextView's contents. You can also set To, Cc and Bcc fields and add some attachments via this class.
You can also check if app can send an email by using:
[MFMailComposeViewController canSendMail];
E.g. if user didn't configure his email accounts, this function will return NO.
More info about sending emails from your app you can find in MFMailComposeViewController Class Reference.

MFMailComposer is not working in iPhone 3GS

I have this piece of code for MFMailComposer working fine in the simulator and iPhone 4, but it crashes on 3GS. What is the reason and what is the way to resolve it?
I checked it with breakpoints. mailPicker is not allocated with memory.
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
// Set the subject of email
[mailPicker setSubject:#"Somebody got place in my sh*t list"];
NSString *emailBody = #"I just added somebody to my s**t list";
// This is not an HTML formatted email
[mailPicker setMessageBody:emailBody isHTML:NO];
// Create NSData object as PNG image data from camera image
NSData *data = UIImagePNGRepresentation([self captureScreen]);
// Attach image data to the email
// 'CameraImage.png' is the file name that will be attached to the email
[mailPicker addAttachmentData:data mimeType:#"image/png" fileName:#"CameraImage"];
// Show email view
[self presentModalViewController:mailPicker animated:YES];
// Release picker
[mailPicker release];
If at least one email account is enabled on the device, the following call should return YES:
[MFMailComposeViewController canSendMail]
Conversely, if all accounts are disabled/removed, it will return NO.

Implementing email with attachments

In my iPhone app, I have a view where I am showing the names of files stored in the Documents directory.
These files are downloaded from a server, and now I want to implement an email function into my application.
My questions are:
Can I attach more than one file, and if so, what is yhe maximum number of files that can be attached?
When I attach a file, do I have to give the location where it is stored?
Assuming you're using the stock MFMailComposeViewController, you can add more than one attachment using addAttachmentData:mimeType:fileName:. You have to attach the raw data, so you'll need to fetch the file from disk and get an NSData representation. Here's an example of how to add to add a UIImage as an attachment:
MFMailComposeViewController *mvc = [[MFMailComposeViewController alloc] init];
mvc.mailComposeDelegate = self;
[mvc setSubject:#"My Subject"];
[mvc setMessageBody:#"My Message Body" isHTML:NO];
NSData *imageData = UIImageJPEGRepresentation(myImage, 1);
[mvc addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"image.jpg"];
[self presentModalViewController:mvc animated:YES];
[mvc release];

Problem with MFMailComposeViewController - User Can't Edit Mail Body - iPhone

I usually use the below code to allow the user to submit feedback on my apps. However for some reason in my OpenGL app the below code has a problem. It opens the email form correctly, however the form is locked - i.e the user can't actually edit the body of the text. Can anybody spot why this is happening ?
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Feedback on Stop That Bomb Free !"];
NSArray *toRecipients = [NSArray arrayWithObject:#"anemail#gmail.com"];
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString *emailBody =
[NSString stringWithFormat:#"Hi Martin, I would like to make the following comment : "];
[picker setMessageBody:emailBody isHTML:YES];
picker.navigationBar.barStyle = UIBarStyleBlack;
[inputController presentModalViewController:picker animated:YES];
[picker release];
From reading your code I can find some things you should change:
Replace:
NSString *emailBody = [NSString stringWithFormat:#"Hi Martin, I would like to make the following comment : "];
With:
NSString *emailBody = #"Hi Martin, I would like to make the following comment : ";
As you are not using any formatting; you dont need to call the class method to create the simple string.
The other thing you can change is the fact that you message does not contain HTML.
So you dont need isHTML:YES.
I have tested this successfully on a sample app.
I imagine the problem is with the view controller presenting the view, rather than the messageUI view.