Afsal Meerankutty
Afsal Meerankutty

Reputation: 366

How to email sqlite database of Iphone app from itself?

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?

Upvotes: 1

Views: 2612

Answers (1)

SPA
SPA

Reputation: 1289

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"];

Upvotes: 2

Related Questions