grabbing data from an internal file - iphone

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

Related

pathForResource dynamic input

I am trying to write code with dynamic input for pathForResource but I get null value for the directory listing. the files I am using are wav files. Here is the code. I have a.wav file in the project folder. What is wrong here? how can I make this work? Please help.
int alphanum = 97;
NSString *alphastring = [NSString stringWithFormat:#"%i",alphanum];
NSString *path = [[NSBundle mainBundle] pathForResource:alphastring ofType:#"wav"];
AVAudioPlayer *theplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theplayer play];

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

How to add CSS file to iphone project in xcode?

I am working on a web based application for iphone, I have created new cssfile to the project this css file doesn't affect the html file, but when I have uploaded the css file a host it worked fine and styled the html
What is the problem ?
edit 1
This the head element .
css.css is working fine and is linked to the html correctly
jquery.mobile-1.0.min.css is NOT !
Despite the both files exist in the same border
Onotha.com
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.0.min.css" />
edit 2
This is the Objective-C code, I have an exception, I think in the last line of code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window makeKeyAndVisible];
NSString *path= [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
// [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
// NSString *path = [[NSBundle mainBundle] bundlePath];
// NSURL *baseURL = [NSURL fileURLWithPath:path];
// [webView loadHTMLString:htmlString baseURL:baseURL];
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"css/jquery.mobile-1.0.min.css" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, path];
[webView loadHTMLString:pageContent baseURL:baseURL];
return YES;
}
edit 3
I got this after using code posted by Srikar
You can load CSS from local project directory
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
detail info check this site.
More elaborate code here -
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"MyCSS" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, actualPageMarkup];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
More info here
I don't know if you have solved this, but I think you have to load in "pageContent" the content of the page instead of the path.
Something like this:
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding];
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, htmlString];
[webView loadHTMLString:pageContent baseURL:baseURL];

NSURL, creating pdfs

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

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.