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"];
Related
I'm trying to access a resource file I added in the app using relative pathing. I've read that I was supposed to use something like this :
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [[mainBundle resourcePath] stringByAppendingPathComponent:#"myFile.txt"];
fh = [NSFileHandle fileHandleForReadingAtPath:resourcePath];
Except this isn't working. With NSLog, I am able to confirm mainBundle isn't null and resourcePath returns something like this:
/Users/tom/Library/Developer/CoreSimulator/Devices/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX/data/Containers/Bundle/Application/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX/MyApp_Demo.app/myFile.txt
I've tried many things, such as adding the name of directory where the resource is located, that would be Ressources/myFile, but nothing is yielding any result.
I'm a total beginner with Objective-C but I have to tinker with legacy code and I have to deal with this, so any help is much appreciated.
Side note:
this is what the project structure looks like from Xcode:
Meanwhile, in Finder, the Ressources directory isn't inside the MyApp directory, rather they're on the same level inside the project directory. I wonder if that could be the problem.
Side note 2:
NSString *filePath2 = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"txt"];
returns (null) after logging in the console.
It is best to use a simulator for this debugging process
Verification
You should check if the file or Resources folder is actually being copied to the right location or not. If you have added the Resources folder, than check it with the below code
NSString *resourcesFolderPath = [[NSBundle mainBundle] pathForResource:#"Resources" ofType:nil];
NSString *fullFilePath = [NSString pathWithComponents:#[demoToursPath,"filename.txt"]];
NSFileManager *manager=[NSFileManager defaultManager];
NSLog(#"Filepath: %#", fullFilePath);
NSLog(#"File Exist: %#", [manager fileExistsAtPath:fullFilePath]);
Once you have confirmed this, you can update your code to match the location and path it needs to be in order to access the file.
Additional Debug
You can also just print the Document Directory for the application and open a finder window and navigate there to see if the Resources folder is added at the right place (if it was added)
NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]
NSLog(#"Document Directory: %#", documentDir);
If you update the question with more info, I would be able to help you better
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'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
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.
I'm trying to load a plist file in my application with
NSBundle* bundle = [NSBundle mainBundle];
NSString* plistPath = [bundle pathForResource:#"CategoryData" ofType:#"plist"];
categoryProps = [[NSArray alloc] initWithContentsOfFile:plistPath];
but the categoryProps array always ends up with 0 objects. I've placed the CategoryData.plist file under the "Supporting Files" dir in my project but I can't figure out how files are arranged in the compiled app.
Can someone point me to docs that describe how the file system of an app is laid out and how to figure out where files are located within the file system?
I forgot to point out that I am using XCode 4 which does not create a resources folder for the project
Your loading code code should work for locating the file within the file system. In a project, I have:
NSString *data = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
What I would do is log the plistPath to the console or inspect it in the debugger, then navigate to that location on disk and determine if the plist ends up where you think it does.
Also, locate your application bundle in /Users/<# Username #>/Library/Developer/Xcode/DerivedData/<# Unique Appname #>/Build/Products/Debug-iphonesimulator/<# Appname #>.app, right click on it and select "Show package Contents". Ensure that you see your plist where you think you should.
You need to place your plist file in the Resources folder. Then you will be able to load and use them like this
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
UPD: In xcode4 you must to place plist files in the "Supporting Files" directory instead of "Resources". And try to use NSDictionary instead of NSArray