pathForResource of file created within app - iphone

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.

Related

Do you need to delete imported files from Documents/Inbox?

I've got an iOS app that imported files from an email attachment.
I've noticed that once i'm finished with it it places the imported file into Documents/Inbox.
Should my app be deleting these files or does the OS eventually get around to clearing them out?
if so, how? i've tried:
[[NSFileManager defaultManager] removeItemAtPath:[self.url path] error:nil];
However it doesn't seem to reference the file in the inbox, even though self.url is the correct path to my import file.
System does not clear imported files, so you should clear them manually when it is necessary, but not to delete the Documents directory.
How to clear the NSDocumentsDirectory you can find here
If you want to delete files from the inbox use the same code adding
...
NSString *path = [NSString stringWithFormat:#"%#/Inbox", documentsDirectory ];
NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:error:&error];
...
Read the reference
From apple doc:
Use this directory to access files that your app was asked to open by
outside entities. Specifically, the Mail program places email
attachments associated with your app in this directory; document
interaction controllers may also place files in it.
Your app can read and delete files in this directory but cannot create new files or write to existing files. If the user tries to edit
a file in this directory, your app must silently move it out of the
directory before making any changes.
The contents of this directory are backed up by iTunes.

file path in iOS 6 gives me null

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

using ios device's local sources when developing a html application

I want to develop an application which works on xcode's uiwebview. But I want to use local sources instead of internet sources. For example I want to import some photos into my project and build it with that photos. Then I want user to download them with the application (which makes sense) (My question is here! :) I want that application to use photos which are downloaded within the application.
I'm hopefull that I can do it with a path changing* trick. But I'm not sure if it's the same for all devices. And I'm not sure what I should write about path.
here is what I mean :
Is it possible?
If it's possible, how can I determine "PATH_OF_FOLDER_IN_IPHONE" path?
Thx for all suggestions & helpful answers.
Add the image as a resource to your project.
Let say the name of the image is footer.png, you get the path with the following code:
NSString *footerPath = [[NSBundle mainBundle]
pathForResource:#"footer"
ofType:#"png"];
NSURL *footerURL = [NSURL fileURLWithPath:footerPath];
You will then replace the source path of the image tag in the HTML file with the full path.

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

Seeing what files are in a folder in my application

I'm kind of new to ios developing and was wondering could I navigate through my own application with NSFileManager.
I have a folder inside my application and would like to see what files are in it and don't want to use hardcoded file names.
Sounds like you need to get to know NSFileManager's contentsOfDirectoryAtPath: error: method (documentation linked for you).
To look within your own application bundle, pass a path of "[[NSBundle mainBundle] resourcePath]". This would give you a path to the "Resources" folder within your application bundle. If you look at the linked documentation, you'll see NSBundle provides a few other potential ways to get paths to interesting locations within your application package.