NSURL, creating pdfs - iphone

I'm trying to create some pdfs from pdfs added to my iphone as an exercise for myself in quartz and just getting better with the language. So I have this so far to get the multiple pdfs on my phone:
NSError *error;
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:&error];
NSArray *onlyPDFs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self ENDSWITH '.pdf'"]];
How can I convert this to a NSURL object, or an array of NSURL objects? I've tried some different things like URLForResource, initWithString, but I don't think those are correct. Thanks.

You could do something like this to generate an NSURL for each PDF in onlyPDFs:
for (NSString *p in onlyPDFs) {
NSString *name = [p stringByDeletingPathExtension];
NSURL *url = [[NSBundle mainBundle] URLForResource:name withExtension:#"pdf"];
}

Related

Reading from text file - Objective C

I am trying to familiarize myself with objective C, and my current goal is to read a list of items in a text file and store them in a NSString array.
Currently this is what I have:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"txt"];
NSData* data = [NSData dataWithContentsOfFile:filepath];
NSString* string = [[NSString alloc] initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
NSString* delimiter = #"\n";
listArray = [string componentsSeparatedByString:delimiter];
I am not sure if this matters, but myList.txt is in my Supporting Files.
At the moment, I only have one item in my list. However I am unable to store even that 1 item into my listArray.
I am sure it is something silly that I am missing, I am just new to Objective C.
EDIT:
I apologize for not mentioning this earlier. I AM NOT receiving any sort of error. My array is just null.
I'm going to suggest a little simplification which might solve your problem since I can't say what your problem is. From the information I'm not sure if you are getting the proper file contents when reading it in or not.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"txt"];
NSError *error;
NSString *fileContents = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
if (error)
NSLog(#"Error reading file: %#", error.localizedDescription);
// maybe for debugging...
NSLog(#"contents: %#", fileContents);
NSArray *listArray = [fileContents componentsSeparatedByString:#"\n"];
NSLog(#"items = %d", [listArray count]);
If the content of the file is just like:
[{"Title":"20","Cost":"20","Desc":""},{"Title":"10","Cost":"10.00","Desc":""},{"Title":"5","Cost":"5.00","Desc":""}]
try this
-(id)readFromDocumentDBFolderPath:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:appFile])
{
NSError *error= NULL;
NSData* data = [NSData dataWithContentsOfFile:appFile];
id resultData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if (error == NULL)
{
return resultData;
}
}
return NULL;
}

iOS: NSUrl Syntax

I currently have this code that works. I have two questions.
How do I set the directory to "Supporting Files"?
How do I use a variable in the file name? I need it to pull an mp3 of any animal based on a variable.
- (void)playAnimal{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/Sheep.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}
I'm very new to Objective-C, so a lot of syntax things are new to me as well as general methods. Thanks for your patience!
Use the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Sheep" ofType:#"mp3"]];
NSURL *url = [NSURL fileURLWithPath:path];
When your resources are copied to the bundle, they do not retain their directory structure. Just identify the resource by name and it will find the path for it.
Just as an FYI, the following works, as well:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Sheep.mp3" ofType:#""]];
NSURL *url = [NSURL fileURLWithPath:path];
This may be useful if you have a set of resources logically grouped together in a plist and you are programmatically reading those out. For example:
// reference to a dictionary with the entry 'Sheep.mp3'
NSString *resourceName = [dictionary objectForKey:item1];
NSString *path = [[NSBundle mainBundle] pathForResource:resourceName ofType:#""]];
NSURL *url = [NSURL fileURLWithPath:path];

Read RTFD data in IOS

I have RTFD data in NSData objects in IOS that I need to read. In MacOSX I just used to read them with NSAttributedString, but now I know that they are not supported in IOS.
attribString = [[NSAttributedString alloc] initWithRTFD:note documentAttributes:&dict];
Is there any way to read only the text of the RTFD data in IOS?
The rtfd in iOS is something like as a directory. If you only want to get the text part, you can try:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"photo" ofType:#"rtfd"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *content = [fileManager contentsOfDirectoryAtPath:filePath error:nil];
NSString *textFilePath = [filePath stringByAppendingPathComponent:#"TXT.rtf"];//you can get the path component from the content; usually it is TXT.rtf
NSError *error = nil;
NSString *pureText = [[NSString alloc] initWithContentsOfFile:textFilePath encoding:NSASCIIStringEncoding error:&error];
NSLog(#"pureText:\n%#",pureText);
Hope it can help.

grabbing data from an internal file

I'm grabbing data from an external file using NSURL with the code below. If i wanted to grab this data from an internal file in my resources folder how would the code be different.
here's the NSURL code:
NSURL *dataUrl = [NSURL URLWithString:#"https://sites.google.com/site/*****/***/file.asc"];
NSString *fileString = [NSString stringWithContentsOfURL:dataUrl encoding:NSUTF8StringEncoding error:nil];
this was my attempt:
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"asc"];
NSString *fileString = [NSString initWithContentsOfFile:path];
thanks in advance
Maybe try stringByExpandingTildeInPath
something like
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"asc"];
path = [path stringByExpandingTildeInPath];
NSString *fileString = [NSString initWithContentsOfFile:path];

How to get List of Images Using NSBundle

I am having List of images in my project i have to get the entire list Using NSBundle, How can i do this
Thank You
This should do it:
NSLog(#"Images in bundle are:");
NSString *imageExtension = #"png";
NSArray *pngPaths = [[NSBundle mainBundle] pathsForResourcesOfType:imageExtension inDirectory:nil];
for (NSString *filePath in pngPaths)
{
NSString *fileName = [filePath lastPathComponent];
NSLog(#"%#", fileName);
}
Just change the imageExtension string for the image type you want.