I wanted to read version information of my application from the plit file.
For this I am using a code as shown below.
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:#"MyApp-Info"
ofType:#"plist"];
// Format output
NSLog(#"%#",myFilePath);
The output
2010-08-31 17:14:10.095 MyApp[8759:20b] (null)
It always returns nil even if I tried to Import an existing file of type, text.txt still return nil, where text.txt is imported in the application.
I have goggled for the problem dont every one recommends to use NSBundel to read an pre imported file, but no use.
Any help, or an better way to read application version.
Got the solution via another link in the stack overflow here.
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
NSLog(#"%#",version);
The reason this doesn't work is because pathForResource looks for stuff inside "MyApp.app/Contents/Resources". The Info.plist file does not reside inside the resources folder, so it's going to return nil if you look for it there. The correct way to get at it is to use the "infoDictionary" method on NSBundle.
Related
I have a file called "0.ballpoint" that all I want to do is store some coordinates with (don't really want to use Core Data because it seem a little excessive). I placed it in my project but when I try doing this:
if ( access("0.ballpoint", F_OK) != -1) {
printf("file exists\n");
}
else {
printf("doesn't exist\n");
}
It says it "doesn't exist". Do I need to put the full path name? And if I do what do I do when I place it on the actual iPhone/iPod Touch?
CoreData is not meant for File access. There are other file API's available for iPhone.
Please go through NSFileManager and NSStream documentation.
For example, if your intention is to check if file exists at certain path,you may use
(add your file to resources)
NSString *filePath = [[NSBundle mainBundle] pathForResource: #"0" ofType: #"ballpoint"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
Prefer not to use low level stuff (C File handles) unless you have a valid reason to do so. Paths are to be carefully chosen (as you cant make assumptions on certain path, which might cease to exist in future).
Having said that, if you prefer to work with C file handling ,
NSString *filePath = [[NSBundle mainBundle] pathForResource: #"0" ofType: #"ballpoint"];
FILE *fileHandle = fopen([filePath cStringUsingEncoding:NSASCIIStringEncoding],"r");
it needs to become a resource to your Application that way the file can be pulled anytime during the use of your application.
Im not too well educated in Ipod Dev, im more of a windows phone 7 developer but i know it cant access it if it is still on your computer so you need to import it into your application some how and call it with the full file path name.
Following is my code I need check whether file present in document directory ,But following API does not return true at any condition but file is get created in document directory. Please check any thing wrong I am doing. I have searched on this other says this API "fileExistsAtPath" should work but it is not working in my case. Please do help me.
NSString *recordFile=[NSString stringWithFormat:#"MyFile.acc",data.uid];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentDr = [documentPaths objectAtIndex:0]; // WHY 0 ?
NSString *uniquePath = [documentDr stringByAppendingPathComponent:recordFile];
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
NSLog(#"File found")
}
else {
NSLog(#"No File to play");
}
Thanks,
Sony
Probably your path is incorrect. What should be the name of the files you are trying to find? If you have #"MyFile1.acc", #"MyFile2.acc" etc (where 1, 2 is the data.uid value) then try to use this:
NSString *recordFile=[NSString stringWithFormat:#"MyFile%i.acc",data.uid];
Try to append a "/" after documentDr when define uniquePath, or
NSString *recordFile = [NSString stringWithFormat:#"MyFile%#", data.uid];
...
if([NSBundle pathForResource:recordFile ofType:#"acc" inDirectory:documentDr])
the first wrong thing i note is the first instruction.
Why do you pass 'data.uid' as an argument to the format string ? Assuming 'data.uid' is some kind of integer, there should be at least '%d' in...
I had a look to Apple Documentation, in particular, that statement which says
The directory returned by this method may not exist.
You could try getting the current directory path using NSFileManager methods, and then testing for file existence (or create a directory at a place you can easily retrieve later).
What about the others paths ? You wrote 'WHY 0 ?', i'm asking the same question as you.Try looping over the array elements, testing each time for file existence.
The code should work. Your file most likely doesn't exist. Did you use capitalisation in your file name right when saving it?
Also right now, I still don't know what the file name is since, as already said, the first line in your initial post doesn't give the format string right.
Before checking try for existance try:
[[NSString stringWithString:#"test"] writeToFile: uniquePath atomically:YES];
That should create a file at the path and you see, that fileExists works.
I tried many things but ended up that I had to delete the app, clean all targets, and then rebuild again. It's weird that somehow my new resources doesn't get copied to the app. It usually happens to files that when I added them I selected "Create folder references for any added folders".
Have been searching for this, I need to have a conditional statement in my objective C code based on the target, but I can't seem to find a direct way to get the target name from the code.
Have been trying to read the icon value from the bundle using:
NSLog(#"Icon File: %#",[[NSBundle mainBundle] objectForInfoDictionaryKey:#"icon"]);
which always return null.
Looking for a suggestion.
Thanks in advance!
Try
NSLog(#"Icon File: %#",[[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleDisplayName"]);
That should give you the name that is displayed under the icon.
i'm new as an iphone developer, as a hobby i'm writting a small game (just for fun).
i would like to store the default configuration inside a xml file. I have the xml file, I know about how to parse the xml document, but.. what i do not know, and that's why i'm here is:
is there a way to say to Xcode: imagine this (xml) file is stored on /documents/ ? or the unique way is to copy&paste the (xml) file here:
~/Library/Application Support/iPhone Simulator/4.1/Applications//documents/
thanks!
fyi: the xml will always be together with the app.
just drag the xml into the file tree (solution explorer) in xcode
then to get the root directory path, just call
[[[NSBundle mainBundle] bundlePath] UTF8String]
that gets it as a char*, for c style string stuff. otherwise just leave out the UTF8String
[[NSBundle mainBundle] bundlePath] to get the NSString. just add your file name to the end
in my experience, it doesn't matter where you dump your xml in the 'solution explorer' it always ends up in the root directory.
better to use plist instead of xml . if u use plist then there is no need to parse it again.
end then use
NSString *theFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:#"documents/YOUR FILE NAME"]
I'm creating a myDb.plist file in my resources folder and trying to read it, but it's not getting read. I'm using the following code.
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"myDb" ofType:#"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];
contentArray is showing null.
What can I try to resolve this?
Make your plist data array and not dictionary. Dictionary also works but i tried with hit and trial. Also, print NSLog your data, if in case you need, to check the input from plist.