NSBundle pathForResource method - iphone

NSURL *modelURL1 = [[NSBundle mainBundle] URLForResource:#"MyTest" withExtension:#"momd"];
NSLog(#"%#", [modelURL1 absoluteURL]);
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyTest" ofType:#"momd"];
NSURL *modelURL = [NSURL URLWithString:path];
NSLog(#"%#", [modelURL absoluteURL]);
The first line is generated by XCode when I use Core Data, but it does not work because the method doesn't work prior to iOS 4. So I need another method, I thought I could use the second one but it returns nil...Why does it not work?
Thanks

The second one doesn't work because path isn't a URL. Use this instead:
NSURL *modelURL = [NSURL fileURLWithPath:path];

Related

NSURL add parameters to fileURLWithPath method

I use this line of code to load a local html file into a web view:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
However I want to add some http parameters to the url with no luck so far.
I've tried this:
url = [url URLByAppendingPathComponent:#"?param1=1"];
But after this a html doesn't load in webview.
Is there a way to load local html file in webview with params ?
Do this:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
NSString *URLString = [url absoluteString];
NSString *queryString = #"?param1=1";
NSString *URLwithQueryString = [URLString stringByAppendingString: queryString];
NSURL *finalURL = [NSURL URLWithString:URLwithQueryString];
NSURLRequest *request = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
[web loadRequest:request];
The most upvoted answer by Prince doesn't always work.
In iOS 7 Apple introduced NSURLComponents class, we can leverage that to securely add a query into NSURL.
NSURL *contentURL = ...;
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:contentURL resolvingAgainstBaseURL:NO];
NSMutableArray *queryItems = [components.queryItems mutableCopy];
if (!queryItems) queryItems = [[NSMutableArray alloc] init];
[queryItems addObject:[NSURLQueryItem queryItemWithName:#"access_token" value:#"token_here"]];
components.queryItems = queryItems;
NSURL *newURL = components.URL;
I use like this way:
NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"xml"]];
If your file included into the Resource folder of project navigator. otherwise you have to set your full path in NSString and use that in your NSURL path.

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

Loading PDF file from Resource directory in iPhone crashes

NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:#"sampleLayout.pdf" withExtension:nil];
This above line warns NSBundle may not respond to -URLForResource:withExtension:
and the app crashes while loading the PDF file from this URL path.
Why dont you try like this..?
NSString *urlPath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webViewOutlet loadRequest:urlRequest];
NSURL *pdfPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"]];
i got working from this above line, i think this is alternative for ipad 3.2 from iphone 4.0 os

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

Can't load html file to webview

Here is my code :
NSString *path1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"NewSouthWales.html"];
NSURL *pageURL = [NSURL fileURLWithPath:path1];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:pageURL];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
I am getting output in my webview as :
like
........
instead of giving me proper look of that file it shows page source code.
Please help ...
Thanks in advance
First prefer pathForRessource ofType
pathNSString *path1 = [[NSBundle mainBundle] pathForResource:#"NewSouthWales" ofType:#"html"];
And then if the file is local why send a request ?
Just use :
NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"NewSouthWales" ofType:#"html"];
NSURL *baseurl = [NSURL fileURLWithPath:path1];
NSData *pathData = [NSData dataWithContentsOfFile:path1];
//Check first if the file path's data is captured. if true, load the html on the web view
if (pathData) {
[webView loadData:pathData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:baseurl];
}