I am programming for iOS using Objective-C
what is the best place (I mean path or local URL) to save my downloaded mp3 files that I download using my code and what is the best place to save the Plist file that contains information about the downloaded songs
Due to the nature of iOS (and app sandboxing), you can only save within your app's document directory. To obtain the path to this directory, you can use:
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
One advantage of the sandbox approach is the fact that when your app is updated on the device, the files in the document directory will remain untouched and hence can be updated by the app as required.
I'd recommend a read of the File System Programming Guide, as it covers this topic (and a lot more), including iCloud file management, etc.
For Document Directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString* docDir = [paths objectAtIndex:0];
For Caches Directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *libDirectory = [paths objectAtIndex:0];
For tmp Directory:
NSString *tmpDirectory = NSTemporaryDirectory();
Related
I want add documents (except images) in email by coding. i know we can access devices image gallery but i want to access documents of the device.
Can we access the documents just like imagepicker?
No you can not access Images from document directory same as device Library (by UIImagePickerView)
But if you want to get particular image from document directory then use following code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:#"NameOFImage"]; // put name of image which you want to get it from document directory
UIImage *myImage = [UIImage imageWithContentsOfFile:newPath];
Here myImage is image that you want to get it from document directory,
I understand that you want a visual file browser so try NSOutlineView.
Check this (similar) question
Using NSOutlineView as a file browser, starting from a given directory
or this tutorial:
http://www.alauda.ro/2012/04/30/nsoutlineview-inside-out/
Or manually handle the files using NSFileManager (but it seems this is not what you are looking for, but anyways...)
http://www.techotopia.com/index.php/Working_with_Directories_in_Objective-C#Getting__a_Directory_File_Listing
Actually, I don't believe there is anything as easy and simple as UIImagePicker for browsing files and folders so I guess the NSOutlineView will be the best choice for you.
I am struggling to find something useful for automatically uploading photos from photo library to my server end.
In my iphone application i wan to automatically upload the photos to server as user captures a new photo.
The way facebook, Dropbox and google plus app doing.
Any help.
Mostly It's a 3 Step Process :
1) Get the photo. Store it to the Documents Directory. Apple has described it very well here : PhotoPicker
2) Fetch the Photo from Document Directory :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"Images"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[dataPath stringByAppendingPathComponent:[NSString stringWithFormat:#"yourImage.png"]]];
3) Now , you can upload your Photo to the Server. The Process is well described Here :
upload image from iphone to the server folder
I am developing a camera app and whatever the pictures that i take using my app, those pictures should get stored in a private app directory.
If I click a button in the 1st view, it should go to the 2nd view showing the images retrieved from my private app directory.
I should be able to select a few of those images in the 2nd view and if I click the next button, the selected images should be displayed in the 3rd view.
So, first of all, how to create a private directory and how to retrieve images from that directory?
And how to select the images and send them to the next view?
Also, I should be able to change the order of those selected images in the 3rd view
simply by dragging them. How to do this as well?
First of all,
If you are thinking to create an app for storing images & using the images in other app, then you are choosing a wrong path. Apple will not allow you to do that.
What you can do is you can store the images in your application provided directories.
For directories, you can follow these directories:
There are three kinds of writable paths to consider - the first is Documents, where you store things you want to keep and make available to the user through iTunes (as of 3.2):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Secondly, and very similar to the Documents directory, there is the Library folder, where you store configuration files and writable databases that you also want to keep around, but you don't want the user to be able to mess with through iTunes:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
Note that even though the user cannot see files in iTunes using a device older than 3.2 (the iPad), the NSLibraryDirectory constant has been available since iPhoneOS 2.0, and so can be used for builds targeting 3.0 (or even earlier if you are still doing that). Also the user will not be able to see anything unless you flag an app as allowing users to modify documents, so if you are using Documents today you are fine as long as you change location when updating for support of user documents.
Last there is a cache directory, where you can put images that you don't care exist for the long term or not (the phone may delete them at some point):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
Note that you have to actually create the Caches directory there, so when writing you have to check and create every time! Kind of a pain, but that's how it is.
Then when you have a writable path, you just append a file name onto it like so:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"SomeDirectory/SomeFile.txt"];
or
NSString *filePath = [cachePath stringByAppendingPathComponent:#"SomeTmpFile.png"];
Use that path for reading or writing.
Note that you can make subdirectories in either of those writable paths, which one of the example string above is using (assuming one has been created).
If you are trying to write an image into the photo library, you cannot use file system calls to do this - instead, you have to have a UIImage in memory, and use the UIImageWriteToSavedPhotosAlbum() function call defined by UIKit. You have no control over the destination format or compression levels, and cannot attach any EXIF in this way.
I Hope it helps you to fulfill your need & also make an understanding about directories.
It is the first time that I have tested my app on an iPad outside of the simulator. I have some files whose path is retrieved using:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
When running in the simulator everything is ok, but on the iPad it seems that the files in the document directory are not uploaded. How can I upload all of these files to the iPad?
iOS devices follow sandbox environment, so every app have its own Documents folder.
To save files in the documents folder, you can try the following:
//This will provide you path for the document folder of your app.
NSString *documentsPath = [NSHomeDirectory stringByAppendingPathComponent:#"Documents"];
Now, you can use this path to append path of your files and save your files.
Please let me know if any query.
I've added a function so that my database resides on the device, however, in the simulator I can't find it on my mac at all. It used to be in Machintosh HD before I used my function.
+ (NSString*)getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:#"bc.db"];
return dbPath;
}
It will be stored at:
~/Library/Application Support/iPhone Simulator/<SDK>/Applications/<UUID>/Documents/bc.db
The <SDK> is a directory that represents the version of the simulator you are running. So, for instance, if you run it in the 4.1 simulator, replace <SDK> with 4.1.
The UUID is generated by the simulator and will be different for each time you install the app (i.e., hit the "Run" button in Xcode). To discover which app is the one you're working on, just enter the various directories and look at name of the application bundle.