In my current app I am adding a data backup and restore feature.
User can create back up of database and can email it.
In restore feature user can import the database from email and can replace the current.
I am able to do create back up and restore of database file.
In restore I am just copying the file from my email to app Documents file replacing current one.
The problem is that in my code for backup of database, sqlite database is converted to NSData. I don't want to to be converted and need the exact sqlite file to be emailed.
Here is the code that I currently use to email sqlite database
-(void)backUpData
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:#"/DemoApp.sqlite"];
NSURL *newUrl = [[NSURL alloc] initWithString:
[txtPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSData *sqliteData = [[NSData alloc] initWithContentsOfURL:newUrl];
//send sqliteData to email composer delegate to attach it as attachment
[self showPicker:sqliteData];
}
How I can email sqlite db without converting it to NSData?
Use dataWithContentsOfFile when creating the NSData object giving the path rather than a url. For example:
NSString *dbPath = [[NSBundle mainBundle] pathForResource:#"Pubs" ofType:#"db"];
NSData *myData = [NSData dataWithContentsOfFile:dbPath];
[mailComposeVC addAttachmentData:myData mimeType:#"application/x-sqlite3" fileName:#"Pubs.db"];
Related
iPhone App
I am currently trying to understand how i can store a file from a URL to the documents directory and then read the file from the documents directory..
NSURL *url = [NSURL URLWithString:#"http://some.website.com/file"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:#"Timetable.ics"];
[data writeToFile:storePath atomically:TRUE];
I got this code from http://swatiardeshna.blogspot.com/2010/06/how-to-save-file-to-iphone-documents.html
I want to know if this is the correct way to do this and i want to know how i can load the file from the documents directory into an NSString..
Any help will be greatly appreciated.
What you have looks correct, to read that file back into a string use:
EDIT: (changed usedEncoding to encoding)
NSError *error = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:storePath encoding:NSUTF8StringEncoding error:&error];
Of course you should change the string encoding type if you are using a specific encoding type, but UTF8 is likely correct.
If you're doing this on your main thread, then no it's not correct. Any sort of network connection should be done in the background so you don't lock up the interface. For that, you can create a new thread (NSThread, performSelectorInBackground:, NSOperation+NSOperationQueue) or schedule it on the run loop (NSURLConnection).
I am using photos saved in photo library in my application.
how can I retrieve path of photos saved in photo library?
I had converted that UIImage into NSData and saved that data in application's sandbox(in one file).Using sqlite , I have created a database and saving file path in database.
Whenever i need image, I retrieve NSData from file path saved in database.
NSMutableData *data=[[NSMutableData alloc]init];
data =UIImagePNGRepresentation([UIImageimageNamed:#"image.png"]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"first.txt"]; [data writeToFile:filePath atomically:YES];
NSString *m=[[#"'"stringByAppendingFormat:filePath]stringByAppendingFormat:#"'"];
NSString *query=[[#"INSERT INTO newtable (image) VALUES (" stringByAppendingFormat:m]stringByAppendingFormat:#")"];
NSLog(#"query....%#",query);
[obj configureDatabase:#"Editerdb.rdb"];//function to configure database
[obj insertInTable:query];// function to insert into db
This code is working.
Is there any simple way to do that?
Well first of all in the first line you are going to have a memory leak. Check the documentation on UIImagePNGRepresentation to for assistance. so remove the the first line of code. and use NSData instead of NSMutableData coz you are not changing the contents of image.
and second of all you are not saving the image path in database but the image itself again for this referrer to documentation on UIImagePNGRepresentation so when to want to image just retrive the NSData object from database and convert the NSData to UIImage using imageWithData:
I am simply creating a student management system in iPhone.
There I need to store student's small images,
Which should be appear in tableView,
Ok, I know how to work with tableView...
how to work with database...
But question is
Where to store images
how can we obtain the path of stored images..
Do i have to store entire images to database...
Or
i have to store relative path of image to database...
What is suggested by You masters?
Thanks in advance for helping me.
You can create image files on the file system of the device in the Document directory. Use something like this:
// Generate a unique user filename
NSString *imageFilename = [NSString stringWithFormat:#"student_image_%#", uniqueIdentifier];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0], imageFilename];
NSData *storage = [image UIImagePNGRepresentation]; // or UIImageJPEGRepresentation
[storage writeToFile:path atomically:NO];
Best Regards,
i want to replace my sqlite DB stores on iphone by a sqlite which is on my server for an update.
how could i do that? i can download it and get it with an NSData object but how do i store it on the iphone and then replace the old one ?
thanks
That's pretty simple. It should just be something like this:
NSData *fetchedData = ...; /* downloaded using NSURLConnection or whatever*/
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"DBName.sqlite"];
[fetchedData writeToFile:filePath atomically:YES];
I need to store certain information while my application is executing and again fetch it at the time the application starts. I tried storing it in XML using GData but didn't succeed.
I used the NSFileHandle it doesn't give me an error but it fails to create a .txt file for read / write purpose. Is there any other way of storing and retrieving the data on the iPhone. Below is my code for NSFileHandle.
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"myFile.txt"];
//NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:#"file://localhost/Users/shraddha/Desktop/info.txt"];
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:#"myFile.txt"];
[fh seekToEndOfFile];
NSData *data = [camName dataUsingEncoding:NSASCIIStringEncoding];
[fh writeData:data];
[fh closeFile];
For Reading
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"myFile.txt"];
//NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:#"file://localhost/Users/shraddha/Desktop/info.txt"];
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:#"myFile.txt"];
if(fh == nil)
return nil;
else
{
NSData *data = [fh readDataOfLength:8];
NSString *retStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
return retStr;
}
You're trying to write to the resource directory which you probably don't have permission to do. Try changing the first line of your code to point to the document directory:
NSArray *savePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSMutableString *savePath = [NSMutableString stringWithString:[savePaths objectAtIndex:0]];
[savePath appendString:#"/myFile.txt"];
Also, you don't need to worry about file handles. NSData is capable of writing itself to disk:
BOOL result = [data writeToFile:savePath atomically:YES];
You can use a sqlite database to store persistent data on the iPhone. Here is a blog post that should get you pointed in the right direction:
http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/
Every iPhone application has at its disposal the ability to read/write name/value pairs which can be very, very useful for storing small information like user preferences. These preferences can also be edited by the user of your application (if you choose).
Another option you have which is more robust than trying to do text file storage and retrieval (I never liked this option, especially with the crappy XML parsing support on the iPhone) is sqlite. sqlite is a really light-weight relational database engine that is included with every iPhone and iPod touch.