file path in iOS 6 gives me null - iphone

I'm using iPhone6 simulator, I'm trying to get a file (any extension pdf of html) by using any of this codes:
NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#.pdf", documentName] ofType:nil];
or
NSString *file2 = [[NSBundle mainBundle] pathForResource:documentName ofType:#"pdf"];
I'm sure that the file in exists in Resources folder and I'm not add the file, I download it from web programmatically and see it in this path
( /Users/myMac/Library/Application Support/iPhone Simulator/6.9/Applications/E60F22DD-7301-48EF-AB25-B9D42FA6AD49/myApp.app) I see the file, but the code does not see it
but this codes sometimes gives me null and some other times gives me the file.
Why is this happen and how to prevent this and make it always gives me the file.
consider that I uses
if(file != nil)
and want to make if file == nil try to open it by using any way.
Thanks in Advance.

Make sure your file is added to project's target. Choose your target then Build Phases->Copy Bundle Resources. The file should be there.
Sometimes it's worth cleaning your project and building from scratch. I have seen cases when file added wasn't copied to simulator/device until clean was performed.

a) If you download anything from the web then the content goes to the document directory of your application.
b) No other application can interact with the your application doc directory.
c) The Bundle path i.e. the groups and files section of your application is read only.
d) No one can sneak your applications PDF unless and until you have allowed access to the document directory of your application.
Hope this helps

Related

iPhone Application Support Folder Include Files BEFORE Building

I have an iPhone app that programmatically gets a path to the Application Support Folder, tests for a file in the application support folder, and then either loads the file or creates a new one depending on the result. This is easy and there are a ton of tutorials on how to do this.
But I can't for the life of me find anything in the ios documentation or online about how to put a file in the Application Support Folder before ever building the app. I tried creating a Library/Application Support in my apps Xcode folder to no avail.
Specifically, I am making a game, and I want to include level packs in the game's Library/Application Support folder BEFORE I build and run the application. Preferably by dragging and dropping the files in Finder. Is this possible?
#Vimal Venugopalan
EDIT:
As Vimal mentioned, I could use [[NSBundle mainBundle] pathForResource:ofType:inDirectory:] method, but this gives a path similar to "~/MyApp.app/MyFolder/MyFile.plist". That is if "~" was the path to the app's home directory. Or more specifically "~" is the path returned by calling the NSHomeDirectory(); function. Where I want to put my files is in "~/Library/Application Support/MyFolder/MyFile.plist"
I want the files in this spot because I want to incorporate level-packs into my game. I want to include some level packs with the app download, and I would eventually like to have additional downloadable level-packs. Now the downloaded level packs definitely have to go in the "~/Library/Application Support/" folder (which I know how to do programmatically), so I would like to include the original level-packs in the same place before building and running the app. It would be so much simpler to have all my level-packs in one place!
You can add these files in the Project and access these files at runtime Xcode will copy them in the Copy Bundle Resource phase. This normally copies into the root of the bundle. To deal with directories see #CocoaFu's answer to this SO question.
Then in the code
NSBundle* bundle = [NSBundle mainBundle] will give you the main bundle
From this you look in directories using pathForResource:ofType:inDirectory: e.g.
NSString* path = [bundle pathForResource:#"file.xml"
ofType:nil
inDirectory:#"a"];
The methods are given in NSBundle class reference also see the Bundle Programming guide
Hope this solves your issue. If not please comment

pathForResource of file created within app

I've implemented a bit of functionality that uploads a file created within the app to an FTP location, however the 'pathForResource' returns an error where it cannot find the filepath for the resource.
This is the code i'm using:
SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:#"ftp://79.170.44.42"]
toUploadFile:[[NSBundle mainBundle] pathForResource:self.htmlFilePath ofType:#"text/html"]];
I've been able to upload files that are packaged with my app, for example, files that are standard 'info.plist etc'. How would I be able to specify the location of a file that is created within the app? Do I need to give a full resource path instead of just referencing the object?
[[NSBundle mainBundle]pathForResource] will only work for files that you added to the XCode Project. You need to look for the file right where you stored it when it was created.

Concept of file manager in iphone

I am unknown of file manager in iphone
Can anyone explain the concept of file manager here
thank you
Quoting from the Apple Low-Level File Management Programming Guide
You use an NSFileManager object to
perform many generic file-system
operations—for example you can:
Create directories and files.
Extract
the contents of files (as NSData
objects)
Change your current working location in the file system.
Copy, move, and link files and directories.
Remove files, links, and directories.
...
An important point to understand is that your application runs in a "sandbox" - it has access only to its own files, not files created by other applications.
The section on the file system in the iOS Application Programming Guide describes the layout of your application and any files it creates.
For example, here's how you might use NSFileManager to open a file named "Defaults.plist" in your application bundle and read it into an NSData object:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Defaults" ofType:#"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];

iPhone: Should I move data from the application bundle to the documents directory?

I have noticed that in the CoreDataBooks example a default database is copied to the documents directory if the file doesn't already exist there:
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:#"CoreDataBooks" ofType:#"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
My question is, does this double the space it takes up?
i.e. There are now 2 databases, one in the bundle one in the documents folder.
I have a much larger database and a whole bunch of images totaling to about 50mb. Is there another way to go about this without copying the data?
In the example this is done so there is a default file to write to. If this is not done, one would have to create the file in code. The reason for this is because the app bundle is in a sandbox where it is forbidden to write to.
If you need the database to be editable, you must move it outside the app bundle.
AFAIK, Apple strongly recommends against modifying files in the bundle. So if the data are read-only (like, most likely, your images), it's OK to keep it in the bundle. A mutable database is a whole another matter.

Is there a shortcut to get to the /Resources/ folder in iPhone development?

Is there a shortcut method much like NSHomeDirectory() and NSTemporaryDirectory to get to the resources folder within your project?
Is the /resources/ folder the best place to be storing a file?
You can't store writable data in your Resources path, since it's inside the app bundle (which is signed). But it's a good place to store readonly data. As Codezy mentions, [[NSBundle mainBundle] resourcePath] is the path to it, but generally the better way to access files in it is [[NSBundle mainBundle] pathForResource:ofType:] and its siblings (...:inDirectory:, etc.) The latter automatically handles localization when appropriate.
Yes, I store an SQL lite db in the resources folder. Here is a sample of how I get the path.
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"ratings.sqlite"];
I store things in the resources folder if I want each new version to overwrite it, else I store it in the documents folder.