Saturday, December 29, 2012

Apple Mountain lion - how to find users directory

I had to navigate to the Users directory on my Apple Mountain Lion OS. 
I saw that the users directory was hidden and was not accessible.

After some googling, i found it to be easy.

1.) Simply open up finder
2.) Press  < shift > + < command >  + G
3.) This will open a open folder dialog box and you can key in the name of the folder.

Ciao.



Friday, December 28, 2012

iOS Attaching a text file to email

Continuing my previous post about creating a text file iOS.
Refer to the post here.

Now that the file is created, you want to read the file and send it as an attachment.

NSString *fileName = @"yourfile.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

//your app directory will be always be the first one
NSString *directory = [paths objectAtIndex:0];
NSString *path = [directory stringByAppendingPathComponent:fileName];

//now get the data here.
//Not sure, if this is a good idea. Especially if the data is //huge
//better idea might be to open a file handle and read it line by //line
NSData *myData = [NSData dataWithContentsOfFile:path];

//email component
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

[mailer addAttachmentData:myData mimeType:@"text/plain",filename:fileName];


//that should be it.

iOS: Appending to an existing text file

I wanted to make an update to one of my iOS Apps.
iOS Book Organizer

Many users have asked me the ability to export all the saved book data in the app. I started researching into how to write data to a text file and then email the file.

I found that it is not that straightforward to append to a text file on iOS. Most examples I read told me to write the data in one swoop.  

I am hesitant in reading all the data to memory and then attempt to write the data in one big swoop. After due research, I think, I have an answer to this problem.

....
>> read data from your sqllite database or from the ui.
>> if there is multiple records in your database, loop through the data.

>> Before you loop through, create the file

//get the documents path
NSArray *documentsPath = NSSearchPathForDirectoriesInDomain(NSDocumentDirectory, NSUserDomainMask,YES);

//your document folder is the first one in the array list
NSString *yourDirectory = [documentsPath objectAtIndex:0];

//create your file here.
NSString *yourfile = [yourDirectory stringByAppendingPathComponent:@"booklist.txt"];

//create a file handle
NSFileHandle *yourHandle = [NSFileHandle fileHandleForUpdatingAtPath:yourFile];


//loop through ur data
//for every iteration

[yourHandle seekToEndOfFile];
[yourHandle writeData:[yourString dataUsinigEncoding:NSUTF8StringEncoding];

//keep writing.
//after all the data is written.

[yourHandle closeFile];

Your text file is now created with your data.

Next post: How to read from this file and attach it to an email.