iPhone Audio Record with Export/Email - iphone

I have searched all over Apples website and the internet as well as Stackoverflow. Does anyone have any sample code or a tutorial of how I can record audio from the built in mic, and then export that audio via email? Please, I really need it for my app. Thanks.

I used code from Apple's SpeakHere example app to record sound from the built-in mic to the wav file "recordedFile.wav", then added the following methods to the view controller to email the wav file as an attachment:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
- (void)mailAttachedWavFile {
MFMailComposeViewController *picker =
[[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"My Wav File"]; // optional
NSString *fileName = #"recordedFile.wav"; // whatever
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *data = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:data mimeType:#"audio/x-wav"
fileName:fileName];
NSString *emailBody = #"Wav format sound file attached."; // optional
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
}
You can make the mailAttachedWavFile method an IBOutlet from a button. Don't forget to declare the controller as a MFMailComposeViewControllerDelegate in the header file.

Related

Find Photo in Directory! (Programmatically)

Holy Dudes from stackoverflow,
MY PROBLEM: I need to send an email with 3 attachments. The first that is the .txt, is going well, no problems with him, but the problem is with the pictures. I capture a printscreen with this:
-(IBAction)screenShot{
maintimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(update) userInfo:nil repeats:YES];
}
-(void)update{
time = time + 1;
if(time == 2){
[self hideThem]; //hides the buttons
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"GRAPH SAVED" message:#"YOUR GRAPH HAS BEEN SAVEN IN THE PHOTO ALBUM" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[maintimer invalidate];
time = 0;
}
[self bringThem]; //brings the buttons back
}
But now i need to get this image i created! I tried using the same code i use for the text:
-(IBAction)sendMail
{
(..)
//HERE IS WHERE THE NAME IS GIVEN
NSString *filename = [NSString stringWithFormat:#"%f.txt", [[NSDate date] timeIntervalSince1970]];
//THIS IS GOING TO STORE MY MESSAGE STUFF
NSString *content = [[NSMutableString alloc]init];
(...) //LOTS OF UNNECESSARY CODE
//HERE IS WHERE IT BILDS THE FILE
[self writeToTextFile:filename content:content];
//HERE IS THE CODE WHERE IT ATTACHES THE FILE
[self sendMailWithFile:filename];
//[content release];
}
-(void)sendMailWithFile : (NSString *)file
{
//THIS IS THE CODE THAT GETS THE TEXT
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"%#/%#", documentsDirectory, file];
NSData *data = [[NSData alloc] initWithContentsOfFile:fileName];
//THIS WAS MY TRY TO GET THE PHOTO ON THE SAME STYLE OF THE TEXT
NSArray *photoPath = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
NSString *photoDirectoryForFirstPhoto = [photoPath objectAtIndex:0];
NSString *photoName_First = [NSString stringWithFormat:#"%#/%#", photoDirectoryForFirstPhoto, /*I DO NOT HAVE THE PHOTO NAME*/#"FirstPhoto"];
NSData *photoData_First = [[NSData alloc] initWithContentsOfFile:photoName_First];
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
if ([MFMailComposeViewController canSendMail])
{
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:#"RK4 data"];
[mailComposer addAttachmentData:data mimeType:#"text/plain" fileName:#"[RK4][EQUATION][TEXT]"];
[mailComposer addAttachmentData:photoData_First mimeType:#"image/png" fileName:#"[RK4][EQUATION][IMAGE]"];
[mailComposer setMessageBody:#"iCalculus Runge-Kutta data." isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
}
else (...)
[mailComposer release];
[data release];
}
So that's my problem. I need to find a way to get the printscreen i took and attach it.
Because i need to come up with all the attaches in the email as soon as he clicks the "SEND MAIL" button.
Thanks.
Rather than saving to your photo library, you should save to documents directory, or tmp directory, to get documents directory you can use the following.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* dd = [paths objectAtIndex:0];
NSString *path = [dd stringByAppendingPathComponent:#"file.png"];
[UIImagePNGRepresentation(screenImage) writeToFile:path atomically:NO];
Take a look here for how to embed the image directly inside the email using base-64, that should be much cleaner than dumping temporary images to the users saved photos album.
Edit:
Category for handling base 64 with NSData, use the download towards the bottom.

Retrieve saved image from app and email

I am succesfully saving an image to my app after the user takes a picture. What I want to do later is, when the user comes back to the app I want them to be able to email the photo as an attachment. I am not having any luck getting the data from the app converted to an image so I can add as an attachment. Can someone point me in the right direction please. Here is where I save the image after they have taken a picture.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//here is the image returned
app.aImage2 = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation( app.aImage2 );
NSString * savedImageName = [NSString stringWithFormat:#"r%#aImage2.png",app.reportNumber];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * dataFilePath;
dataFilePath = [documentsDirectory stringByAppendingPathComponent:savedImageName];
[imageData writeToFile:dataFilePath atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
And here is where I need to attach it.
//this is inside my method that creates an email composer view
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the user did with your email sheet
//how would i attach the saved image from above?
This includes code that Mike mentions here:
How to add a UIImage in MailComposer Sheet of MFMailComposeViewController
Also, other portions are lifted from Sagar Kothari's answer here:
Sending out HTML email with IMG tag from an iPhone App using MFMailComposeViewController class
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Dismiss image picker modal.
[picker dismissModalViewControllerAnimated:YES];
if ([MFMailComposeViewController canSendMail]) {
// Create a string with HTML formatting for the email body.
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:#"<html><body>"];
// Add some text to it.
[emailBody appendString:#"<p>Body text goes here.</p>"];
// You could repeat here with more text or images, otherwise
// close the HTML formatting.
[emailBody appendString:#"</body></html>"];
NSLog(#"%#", emailBody);
// Create the mail composer window.
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
// Image to insert.
UIImage *emailImage = [info objectForKey:UIImagePickerControllerOriginalImage];
if (emailImage != nil) {
NSData *data = UIImagePNGRepresentation(emailImage);
[emailDialog addAttachmentData:data mimeType:#"image/png" fileName:#"filename_goes_here.png"];
}
[emailDialog setSubject:#"Subject goes here."];
[emailDialog setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[emailBody release];
}
}

MFMailComposeViewController bringing up NSException

I have done a search for this problem, and have not found an answer, which is leading me to write my firs tquestion here.
The problem is when I press the button which calls the metho below, I get an exception on this line: [self presentModalViewController:mailViewController animated:YES]; and the exception is:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x20f6c'
In addition, I am running my app on the simulator if that helps.
I got the magority of the code from here.
Here is my code (with the email address blanked out):
// set up the email address array
NSArray *email = [[NSArray alloc] initWithObjects:#"foo", nil];
// Set up the view controller
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
// Set up the email to send to
[mailViewController setSubject:#"foo"];
[mailViewController setToRecipients:[email objectAtIndex:0]];
// Get the path to the plist
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *path = [documentsPath stringByAppendingPathComponent:#"foo.plist"];
// Get the plist from the path
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailViewController addAttachmentData:myData mimeType:#"plist" fileName:#"foo"];
// Fill out the email body text
NSString *emailBody = #"Attached PLIST file";
[mailViewController setMessageBody:emailBody isHTML:NO];
// Release the array
[email release];
if (mailViewController != nil) {
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
}
What appears to be causing the exception?
In setToRecipients you have to specify an array, not a string. So you could pass your email-array.
-(IBAction)btnemail:(id)sender{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"subject"];
NSString *emailid=btnEmail.titleLabel.text;
NSArray *toRecipients = [NSArray arrayWithObject:emailid];
[picker setToRecipients:toRecipients];
NSString *emailBody = #"";
[picker setMessageBody:emailBody isHTML:NO];
if ([MFMailComposeViewController canSendMail]) {
[self.parentViewController presentModalViewController:picker animated:YES];
}else {
}
[picker release];
}

the ui of "New Message" of Mail app?

How to implement the UI of "New Message" of Mail app in iPhone.?
Use the MFMailComposeViewController class.
Don't worry about UI to integrate mail in your app. Everything will be taken care by MFMailComposeViewController Framework. Add this framework and Write the below code
#import <MessageUI/MessageUI.h>
#interface ViewReminderViewController_iPhone : UIViewController
<MFMailComposeViewControllerDelegate>
{
UiButton *mailButton;
}
- (IBAction)EmailButton:(id)sender;
#end
#implementation ViewController
- (IBAction)EmailButton:(id)sender
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Your EMail Subject"];
//SET UP THE RECIPIENTS (or leave not set)
//NSArray *toRecipients = [NSArray arrayWithObjects:#"first#example.com", nil];
//[picker setToRecipients:toRecipients];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
//[picker setCcRecipients:ccRecipients];
//NSArray *bccRecipients = [NSArray arrayWithObjects:#"four#example.com", nil];
//[picker setBccRecipients:bccRecipients];
//ATTACH FILE
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"MediaFiles"];
path = [path stringByAppendingPathComponent:MyFileName];
NSLog(#"Attaching file: %#", path);
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) //Does file exist?
{
NSLog(#"File exists to attach");
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/octet-stream"
fileName:#"DesredFileName.mov"];
}
//CREATE EMAIL BODY TEXT
NSString *emailBody = #"Your Email Body";
[picker setMessageBody:emailBody isHTML:NO];
//PRESENT THE MAIL COMPOSITION INTERFACE
[self presentModalViewController:picker animated:YES];
[picker release];
}
Delegate To Clear Compose Email View Controller
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES]; //Clear the compose email view controller
}

embed mail compose in an iPhone app

I want my app to send mail. I can use the mailto: URL scheme, but it terminates my app when launching iPhone mail. The newsreader from The Independent (British paper) seems to bring up a mail compose view within the app. When you send or cancel, the app reappears immediately.
Does anyone know how to do this?
thanks,
You need to use 3.0 Message UI Framework!
#import <MessageUI/MessageUI.h>
#interface ViewReminderViewController_iPhone : UIViewController
<MFMailComposeViewControllerDelegate>
{
UiButton *mailButton;
}
- (IBAction)EmailButton:(id)sender;
#end
#implementation ViewController
- (IBAction)EmailButton:(id)sender
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Your EMail Subject"];
//SET UP THE RECIPIENTS (or leave not set)
//NSArray *toRecipients = [NSArray arrayWithObjects:#"first#example.com", nil];
//[picker setToRecipients:toRecipients];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
//[picker setCcRecipients:ccRecipients];
//NSArray *bccRecipients = [NSArray arrayWithObjects:#"four#example.com", nil];
//[picker setBccRecipients:bccRecipients];
//ATTACH FILE
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"MediaFiles"];
path = [path stringByAppendingPathComponent:MyFileName];
NSLog(#"Attaching file: %#", path);
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) //Does file exist?
{
NSLog(#"File exists to attach");
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/octet-stream"
fileName:#"DesredFileName.mov"];
}
//CREATE EMAIL BODY TEXT
NSString *emailBody = #"Your Email Body";
[picker setMessageBody:emailBody isHTML:NO];
//PRESENT THE MAIL COMPOSITION INTERFACE
[self presentModalViewController:picker animated:YES];
[picker release];
}
Delegate To Clear Compose Email View Controller
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES]; //Clear the compose email view controller
}