I have tried this in a variety of ways and yet it never pops out my file as a string. Any help would be much appreciated. Perhaps a fresh set of eyes can get it.
static NSString *JSParse;
JSParse = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"nameFinder" ofType:#"js" ] encoding:NSUTF8StringEncoding ];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"nameFinder" ofType:#"js"];
NSLog(filepath);
if (filepath)
{
NSString *someJscript = [NSString stringWithContentsOfFile:filepath];
NSLog(someJscript);
}
NSLog(#"StartParse");
NSLog(JSParse);
NSLog(#"End Parse");
The end goal is to use javascript to parse HTML from a website that isn't mine and yes it sucks. (Parsing although my code is suspect too at this point);
The solution is here: https://stackoverflow.com/a/2018644/526547
You don't need to change your .js to .txt if you use this method.
The Reason for this has something to do with it being a JS file extension. Changing it to a txt file invariably fixes the problem. I'm not sure why this is so but i'm assuming it's some compilation issues.
Related
I want to get the source code from .m file so I use this way:
NSString *path = [[NSBundle mainBundle] pathForResource:#"ViewController" ofType:#".m"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(#"%#", content);
However the console print "null".
I wonder if there any way we can use to get .m file's content even if use private method.
If you're copying the actual source file inside your application's bundle and including it, then sure, that'll work, although I don't know why you'd want to do that.
If you're wondering if you can decompile a binary file back to Objective-C code, then no, you can't do that—at least not simply. If you have good enough skill with assembly language, of course, you can read a disassembly of the code, figure out what the original Objective-C did, and use that knowledge to recreate it.
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.
After I build an app with sqlite. I have installed SQLite manager in Firefox. Doesn´t help because I really don´t know where is that file. I tried many ways.
And finally I try to find this file
_databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:#"myUsers.db"]];
To open with MesaSQLite
Still have the same problem. Where is my file.
Here is the last way I have used:
/Users/{YOUR NAME}/Library/Developer/CoreSimulator/Devices/{DEVICE ID}/data/Containers/Data/Application/{APPLICATION ID}/
I still can not find it. I have open almost every project folders.
Please help me.
Dont open Library on MacintoshHDD. You need to open Library on your USERName Folder. There,normally library folder is hidden. You need to follow the following to see the hidden files.
Or simply copy paste the path into the search finder on your mac.It will take you directly.
The long way to show hidden Mac OS X files is as follows:
Open Terminal found in Finder > Applications > Utilities.
In Terminal, paste the following:
defaults write com.apple.finder AppleShowAllFiles YES.
Press return.
Hold 'alt' on your keyboard, then right click on the Finder icon in the dock and click Relaunch.
You should be able to find this using this method :
- (NSURL *)applicationDocumentsDirectory
{
NSLog(#"%#",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Just copy paste whatever is logged in navigate into the folder where you stored your sqlite file
The end result should look like this :
~/Library/Developer/CoreSimulator/Devices/4D2D127A-7103-41B2-872B-2DB891B978A2/data/Containers/Data/Application/0323215C-2B91-47F7-BE81-EB24B4DA7339/Documents/MyApp.sqlite
Please note that the long ID's will obviously be different, as well as the file name.
You just following line of code to Log.
NSString *databasePath = [[NSBundle mainBundle] pathForResource:#"myUsers" ofType:#"db"];
Or use below code.
NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:#"myUsers" ofType:#"bundle"]];
NSLog(#"%#", bundle);
NSString* test = [bundle pathForResource:#"myUsers" ofType:#"db"];
NSLog(#"%#", test);
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:#"Root" ofType:#"plist"]];
NSLog(#"%#", dict);
I am using the Core Data Editor a lot for my iOS projects. It is compatible with Mac and iOS applications and support XML, SQLite and binary stores, etc. It is free. :-)
http://thermal-core.com/CoreDataEditor/
I am trying to construct a library in IOS.
In my library ,I use a method -(void)loadNewData to load some data EX:test.csv.
But when I export my library and let the other app use my library to excute the loadNewData method.
I found it can not be load correctly. It seems that because there is not such a file test.csv in the app.
How should I revise my code? Or where should I notice when I export the library?
Thank you for watch my question.
And sorry for my poor English.
Here is the a part of code of the loadNewData:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *Rawsurveys=[resourcePath stringByAppendingPathComponent:#"test.csv"];
NSString *RawsurveyResults;
if([[NSFileManager defaultManager]fileExistsAtPath:Rawsurveys])
{
NSLog(#"find file");
NSFileHandle *RawfileHandle=[NSFileHandle fileHandleForReadingAtPath:Rawsurveys];
RawsurveyResults=[[NSString alloc]initWithData:[RawfileHandle availableData] encoding:NSUTF8StringEncoding];
[RawfileHandle closeFile];
}
You could probably create a bundle within your library and add you test.csv there.
Then you can access your test.csv from your own bundle.
Example of how I use images from my bundle. [UIImage imageNamed:#"myBundle.bundle/myImage"]. You could use the somewhat same for your file
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"];