How to remove the path from an attachment when calling addAttachmentData? - ios

When adding an attachment to an email, the file name gets it's complete path.
For a file located in:
/var/mobile/Applications/C3BBAA5F-07FE-4E26-9661-CB492E06BD2E/Documents/
I'm getting as a result, a file named:
_var_mobile_Applications_C3BBAA5F-07FE-4E26-9661-CB492E06BD2E_Documents_Clock.sqlite
When I need my filename to be:
Clock.sqlite
Here's my code:
NSString *path = [self getDatabaseFilePath];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/x-sqlite3" fileName:path];
How can I get the attachment to have only the filename and extension, without the complete path?
Thank you!

This should do what you want:
NSString *path = [self getDatabaseFilePath];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/x-sqlite3" fileName:[path lastPathComponent]];

Related

convert url into NSData

I want to upload voice recording on server.
My file url is:
file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/87C457FA-1A9F-4CA9-A651-6A3D411A0B7E/Documents/myAudio0.mp3
My Code is:
NSData* data = [NSData dataWithContentsOfURL:audioURL options:NSDataReadingUncached error:&error];
But the data is nil.
Please try below code
NSString *audioURLString = #"file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/87C457FA-1A9F-4CA9-A651-6A3D411A0B7E/Documents/myAudio0.mp3";
NSString *sendStr = [[audioURLString absoluteString] stringByReplacingOccurrencesOfString:#"file:///private" withString:#""];
NSData *data = [[[NSData dataWithContentsOfFile:sendStr]]];
Good luck.....
Use this code for get data from Document folder:
if([[NSFileManager defaultManager] fileExistsAtPath:filepath)
{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:filepath];
}
else
{
NSLog(#"File not exits");
}
You just need to do:
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"test" withExtension:#"png"];
NSString *path = [imgPath absoluteString];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:path];
Try this
NSData* data =[[NSData alloc]init];
data = [NSData dataWithContentsOfFile:[NSURL URLWithString:path]];
After run:
file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/"87C457FA-1A9F-4CA9-A651-6A3D411A0B7E"/Documents/myAudio0.mp3
The Bold text "87C457FA-1A9F-4CA9-A651-6A3D411A0B7E" may change;
You should always use the method to get the path in sandBox:
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:#"myAudio0.mp3"];
You can use this:
NSData *data = [NSData dataWithContentsOfURL:yourURL];

Is it possible to save a picture or put a picture in a resource folder that can be retrieve?

I have this picture that I manually added to my project and added called like so.
NSString *path = [[NSBundle mainBundle] pathForResource:#"picture" ofType:#"png"];
But I am wondering if it is possible to take a picture and put it in path for resource that can be retrieve like the code above? My overal goal is to upload my picture to a server with the following code.
[request addFile:path forKey:#"upload"];
Any tips or help will be deeply appreciated.
You can't add a picture (image) to the app bundle, however you can save an image to your apps sandbox. E.g.:
NSData *sourceData = UIImagePNGRepresentation(image);
BOOL result = [sourceData writeToFile:savePath atomically:YES];
where image is a UIImage and savePath is a NSString to the location in which you want to save the image.
If you want to save the image as JPEG (instead of PNG), use UIImageJPEGRepresentation.
You can add the image first to the document directory and get back the url of image like this:
NSData *thumbData = UIImagePNGRepresentation(thumbImg);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imgUrl = [documentsDirectory stringByAppendingFormat:#"/image.png"];
[thumbData writeToFile:imgUrl atomically:NO];
Now image can be getback with the imgUrl like this:
UIImage *img=[UIImage imageWithContentsOfFile:imgUrl];
Hope this helps!
If you are having the NSData of taken picture..
NSData *imageData=UIImageJPEGRepresentation(image);
[request addData:imageData withFileName:#"imagepicture.jpeg" andContentType:#"image/jpeg" forKey:#"upload"];
FileName anything you can give..
If it is PNG image then
NSData *imageData=UIImagePNGRepresentation(image);
[request addData:imageData withFileName:#"imagepicture.png" andContentType:#"image/png" forKey:#"upload"];
Hope it may help you...

ios , Attach Audio file aac format to Email

i try to send a audio record by file aac (kAudioFormatMPEG4AAC) format but the file attached it's doesn't send
*myString = file://localhost/private/var/mobile.......
here is my code
MFMailComposeViewController *picker =
[[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"My Audio File"];
NSString *fileName = #"Rec.aac";
NSString *documentsDirectory = myString ;
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *data = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:data mimeType:#"audio/aac"
fileName:fileName];
NSString *emailBody = #"AAC format sound file attached.";
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
NSData *data = [NSData dataWithContentsOfFile:path];
You're passing in a file:// URL, which won't be understood by that method. That method expects just a standard file path, i.e. /private/var/mobile/.
You could simply use the file path, but if you're only provided with a URL form string, you can create a URL object and use that with NSData.
NSURL* fileURL = [NSURL URLwithString:myString];
NSError* error = nil;
NSData* data = [NSData dataWithContentsOfURL:fileURL options:0 error:&error];
if (error) {
NSLog(#"Unable to load file from provided URL %#: %#", myString, error);
}

How to add gif to attachment in iPhone mail?

I add .jpg this way:
UIImage *sentPic = self.image;
NSData *imageData = UIImageJPEGRepresentation(sentPic, 1);
[picker addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"pic.jpg"];
I change it to "image/gif".But it doesn't work fine. Th Gif is just a still image in the mail.
NSData *imageData = [[NSData alloc] initWithContentsOfFile:pathToGifFile];
[picker addAttachmentData:imageData mimeType:#"image/gif" fileName:#"pic.gif"];
[imageData release];
This would add it as attachment, but I'm not sure if that's what you want, or you want to display the image inside the message?
Have you ever tried HTML code in your email like this:
NSString *emailBody = #"It is a gif email!";
emailBody = [EmailBody stringByAppendingString:#"<img src='http://xx.xx.pic.gif'>"];
[picker setMessageBody:emailBody isHTML:YES];
The code I haven't tested, you can have a try. Tell us if it works.
I just tested this. Works great:
NSString *pathForGif = [[NSBundle mainBundle] pathForResource: #"<your file name>" ofType: #"gif"];
NSData *gifData = [NSData dataWithContentsOfFile: pathForGif];
[mailController addAttachmentData:gifData mimeType:#"image/gif" fileName:#"EmailGIF.gif"];
[self presentModalViewController:mailController animated:YES];

how to attatch a file to the mail using default mail composer

i hav a file with some data and i want to attach this file to mail, and i am using the default mail composer ...
any idea how to attach the file to mail before sending to receipents....
Thanks in advance....
Example with an attached image:
NSData *myData = UIImagePNGRepresentation(myImage);
[mailComposer addAttachmentData:myData mimeType:#"image/png" fileName:#"MyImage"];
NSString *path = [[NSBundle mainBundle] pathForResource:#"bg" ofType:#"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[controller addAttachmentData:myData mimeType:#"image/jpg" fileName:#"Attachment-1"];
[self presentModalViewController:controller animated:YES];

Resources