I'm building an application and want to load the cocoapods auto-generated acknowledgements markdown file into an NSString to be displayed in my application. I though it would be as simple, as doing this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Pods-acknowledgements" ofType:#"markdown"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
But this doesn't work.... Is there a way to access this file?
It doesn't look like this file is automatically copied into your project bundle.
You need to add this file to your Copy bundle Resource build phase. It's also worth nothing that the file name actually contains your project's name Pods-<PROJECT_NAME>-acknowledgements
In practice you may want to make a symbolic link in your project that points to the generated file in the Pods directory to ensure that the newly generated file is used each time
Related
I have to share my OS X app, all the paths I have used for files used by project are linked to my username and the structure of my computer.
Is there a way to have paths related to my project so that once my project is shared the user may not get in troubles caused by 'file not found'.
I would move the used files of the project, into the project but then I don't know how to let this happen:
actual paths, what I use now:
/Users/???username???/XCode/projectName/fileName.txt
what I would like to use in my code:
function(path: fileName.txt)
how don't know how to make the paths this short, not caring about the users directories since the files I'm going to use are all inside my project.
I am very confused. Thank you.
There is actually an easy way to read files from your project directory:
NSError *error = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"txt"];
NSString *dataContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
So in dataContent you have the content of the file as an NSString instance.
I have images.xcassets listed ounder copy bundle resources, and I did try to just state the file name by itself: MSB_big_icon , before trying to add the path within images.xcassets.
Can anybody tell me what I'm doing wrong?
NSString *path = [[NSBundle mainBundle]pathForResource:#"/Raymio_android_images/MSB_big_icon.imageset/MSB_big_icon" ofType:#"png"];
NSLog(#"path: %#", path);
MSBIcon *tilecon = [MSBIcon iconWithUIImage:[UIImage imageWithContentsOfFile:path] error:&error];
David Ansermot is right that xcassets is a much better approach and strongly preferred. If you can't use that (running on older versions of iOS for instance), still put everything in one directory and use imageNamed:. This has significant caching benefits over hand-loading the file.
An asset catalog (xcassets) is a (relatively) new, unified way of managing image resources. The images are no longer accessible as separate files on the disk. Instead, imageNamed: consults the asset catalog and fetches the correct asset.
Prior to asset catalogs (and still, for non-images), assets were stored in localized directories. All of your unlocalized assets would be put into a directory called Resources (no matter where those files might appear to be in your source tree, and no matter how those files might be arranged in your Xcode folders). Localized files would be stored in directories like English.lproj or French.lproj. When you make NSBundle calls to load MyImage, it looks at each localized directory in the order the user has configured, and if it cannot find it in any of those directories, it looks in Resources.
Now it is possible to store full directories as "a resource" by marking them as directory references in Xcode. In that case, the whole directory would be copied into Resources or the appropriate localized directory. In order to find files inside such a directory you can use the ...inDirectory: version of the NSBundle methods.
So most of the time, you want to just use imageNamed:, which is going to fetch things out of the asset catalog if available, and then search localized directories, and then look in Resources. If you need to find a non-image, or if for some reason you want the real path to the file, you can compute it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon" ofType:#"png"];
And if that resource were in a directory tree (because it was a directory reference in Xcode), you can access it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon"
ofType:#"png"
inDirectory:#"Raymio_android_images/MSB_big_icon.imageset"];
Here's a code exemple from one of my apps :
NSString *appKey = #"Applications__GENERIC";
NSString *path = [[NSBundle mainBundle] pathForResource:appKey ofType:#"plist"];
appData = [NSDictionary dictionaryWithContentsOfFile:path];
The "Applications__GENERIC.plist" is stored like this :
Other solutions :
Use the images.xcassets.
Then in your code to load an image, use the code :
UIImage *image = [UIImage imageNamed:#"MyImageWithoutExtension"];
Don't put any path or extension, only the image's name
Try using this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon" ofType:#"png" inDirectory:#"Raymio_android_images/MSB_big_icon.imageset"];
What you can also do to debug is to print out
[[NSBundle mainBundle] bundleURL]
Then navigate to that folder and see if the folder structure corresponds to the path you use.
I just struggled with this today, hope it works out for you too.
I really like to embed font-awesome (http://fortawesome.github.io/Font-Awesome/) in my iOS application to use it in a UIWebView.
I added font-awesome to my project through File -> Add Files to "project"... I checked both options "Copy items into destination group's folder" and "Create folder references for any added folders". This has resulted in a "font-awesome-4.0.3" folder in the root of my project.
Up till now I haven't figured out how to reference the font-awesome css from the HTML in my UIWebView. As a test, I wrote some code that loads one of the css files and logs its contents:
NSString *path = [NSBundle pathForResource:#"font-awesome" ofType:#"css" inDirectory:#"font-awesome-4.0.3/css"];
NSString *css = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(#"%#", css);
Which gives the following output:
2014-02-08 17:55:04.364 project[7729:70b] (null)
Could it have something to do with the dots in the directory and file names used by font-awesome?
Oh and btw, I know there are projects like https://github.com/alexdrone/ios-fontawesome, but those can't be used in a UIWebView (or can they?)
Edit: I have some more information, stringWithContentsOfFile throws a NSFileReadInvalidFileNameError.
Ok, this had nothing to do with special chars whatsoever. Stupid mistake:
NSString *path = [NSBundle pathForResource:#"font-awesome" ofType:#"css" inDirectory:#"font-awesome-4.0.3/css"];
should have been:
NSString *path = [[NSBundle mainBundle] pathForResource:#"font-awesome" ofType:#"css" inDirectory:#"font-awesome-4.0.3/css"];
I am trying to read entire .m file in a string using code:
NSString* fullFileName = #"file.m";
NSString* fileName = [[fullFileName lastPathComponent] stringByDeletingPathExtension];
NSString* extension = [fullFileName pathExtension];
NSString* filePath =[[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSString* fileTextContent=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
But it returns null.
Is it possible to read .m file from bundle in a string in iOS?
It's possible, but you should make sure the .m file is in the bundle first (they aren't by default).
Right click on the app under the products folder in Xcode, select "Reveal in Finder", then right click on the app in finder and select "Show Package contents" to see what's in the bundle.
If you need to add it, you can do that under the project settings in xcode > build phases > copy files phase.
Including .m files in the bundle would be somewhat strange though. There's almost certainly a better way to do whatever it is you're trying to do. Maybe if you went into more detail on what that is, people could suggest them.
Currently using:
NSString* path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"txt"];
This only works however after I manually add the file into the application bundle.
I can use the documents directory but that's even worse.
After resetting the sim, the file goes away. How do I get it to stay?
How would I write out the file from somewhere to there? Like from a file in source? I don't want to alloc a several megabyte NSString object.
Credit goes to Lyle42 on freenode irc (#iphonedev):
I wasn't aware this field even existed. By adding any file into the copy files build phase (under build phases), they persist across builds.
Then this code:
NSString *_filePath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"txt"];
NSLog(#"%#",_filePath);
NSData *_binary = [NSData dataWithContentsOfFile:_filePath];
NSString *_fileContents = [[NSString alloc] initWithData:_binary encoding:NSUTF8StringEncoding];
Works just great. (loads contents of file into an NSString).
You can use this to get your path for the file
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [documentPaths objectAtIndex:0];
filePath = [path stringByAppendingPathComponent:file_name];
you can get the file from your app bundle using
NSString *pathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:file_name];
I can use the documents directory but that's even worse.
Not really. The documents directory is persistent, and the recommended directory for essential documents created by your application. In addition, it's backed up by iTunes, and can be specified for iCloud backup. The app bundle just can't compete with that!
After resetting the sim, the file goes away. How do I get it to stay?
You can't, once the simulator has been "reset" (and I mean a hard wipe of all data and apps), nothing is saved. In between test builds, however, data copied out of the app bundle should survive unless you're storing things in /tmp. In addition, objects in the bundle are "refreshed" (recopied) in the build phase, meaning your resource isn't wiped out, merely reset to the one specified in your project.
How would I write out the file from somewhere to there? Like from a
file in source? I don't want to alloc a several megabyte NSString
object.
To read the file into memory, it would have to turn into a "several megabyte NSString" eventually, but if you really want to add a little IO to your plans for a massive string object, have a look at NSFileManager.