Problem with NSSearchPathForDirectoriesInDomains - iphone

I am trying to work directories. Unfortunately i get a non-writeable directory when I run NSSearchPathForDirectoriesInDomains. What I get is:
/Users/me/Library/Application Support/iPhone Simulator/User/Documents
When I run other people's examples I get:
/Users/me/Library/Application Support/iPhone Simulator/User/Applications/6958D21C-C94B-4843-9EF1-70406D0CA3A3/Documents
which is writeable.
The snippet of the code used is
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(documentsDirectory);
What do I need to do allow me to get the same long directory structure?

That long path with the GUID is the documents path for your app, and is expected behavior.
Not sure what your code looks like, but getting the path to your app's document directory should be something like:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
(From Mark/LaMarche p. 331)

since it searches for the "object at index:0"
NSString *documentsDirectory = [paths objectAtIndex:0];
there could be a directory starting with a letter smaller than "d" for documents. Which becomes the "object at index:0". I know this might not be possible but It could be true as well.

Related

file exist in Document folder but not for Xcode

I have a strange problem, this code check if a file exist inside document folder:
- (BOOL) checkIfFileExist:(NSString *)path {
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentsPaths objectAtIndex:0];
NSString *fileDaControllere = [documentsDirectory stringByAppendingString:path];
if ([[NSFileManager defaultManager] fileExistsAtPath:fileDaControllere]) {
NSLog(#"exist");
return YES;
}
else {
NSLog(#"not exist");
return NO;
}
}
the problem is that I get alway file not exist while the file exist (in this case the path is Style.css)! where is the mistake?
The path seems to be correct:
Path: /Users/kikko/Library/Application Support/iPhone Simulator/6.0/Applications/38161AFA-2740-4BE2-9EC4-C5C6B317D270/Documents/Style.css
Here you can see the path on xcode and real path
http://www.allmyapp.net/wp-content/iFormulario/1.png
http://www.allmyapp.net/wp-content/iFormulario/2.png
The problem may be in the fact that you just append the file without with check if there is a path delimitor:
NSString *fileDaControllere = [documentsDirectory stringByAppendingString:path];
Thus you would become something like ../DocumentsStyle.css but it should be ../Documents/Style.css.
NSString has a special method for appending path components stringByAppendingPathComponent:
NSString *fileDaControllere = [documentsDirectory stringByAppendingPathComponent:path];
At the end I solved this issue, the strange think is that I don't know how I solved it,
the first problem is that I pass to checkIfFifFileExist the absolute path while I need to pass it relative path, and the the function trasform it to absolute path (my big big error), after this I think the problem is the use of "/", I delete all and rewrite all the code and I doing some test.
I copy folder from bundle:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *folderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"/icone"];
NSString *iconePath = [documentsDir stringByAppendingPathComponent:#"/icone"];
[[NSFileManager defaultManager] copyItemAtPath:folderPath toPath:iconePath error:nil];
Then I make the path of my image in this way:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *imagePath = [documentsDir stringByAppendingPathComponent:self.objMateria.iconaMateria];
and now the file exist, a strange thing is that if:
self.objMateria.iconaMateria = /icona/Home/fisica.png
or
self.objMateria.iconaMateria = icona/Home/fisica.png
nothing change, I see the image, while I think that one of this has a wrong path...

using NSSearchPathForDirectoriesInDomains properly

I am fairly new to archiving an object I have. I have a dictionary that I want to archive.. so first I got the path by doing:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dictionaryPath = [paths objectAtIndex:0];
so is the next step to append this dictionaryPath with a .txt? Or should I create something first at this directory? What is then the data type?
I want to be able to use it for:
[NSKeyedArchiver archiveRootObject:locationDic toFile:dictionaryPath];
Where dictionaryPath is the path to store this NSDictionary
NSSearchPathForDirectoriesInDomains will give you an array of directory paths. So if you take the first one, you need to append a filename (whatever filename you'd like, with whatever extension you'd like), and then use that path as the location to write out to.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dictionaryFilePath = [documentsDirectory stringByAppendingPathComponent:#"Filename.extension"];
[NSKeyedArchiver archiveRootObject:locationDic toFile:dictionaryFilePath];

NSSearchPathForDirectoriesInDomains returning empty path?

When would NSSearchPathForDirectoriesInDomains on iOS ever return 0 paths if I have the following:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"paths: %d", [paths count]);
if ([paths count] > 0)
{
// path exists and is the 1st item in the array
}
else
{
// path doesn't exist. Under what conditions would it not exist?
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Add the "s"
I suspect this is just defensive coding. I can't see why that exact call would ever return 0 paths, but the NSSearchPathForDirectoriesInDomains() method might do with different params passed to it.
Also, even if the path exists, the docs say that doesn't guarantee that the actual dir exists, so ideally you'd need to do another check there too, although for NSDocumentDirectory I suspect most people, myself included, just assume it will be there - can't see why it wouldn't be..
I think it is not crash at there. just compiler stop at the wrong postion.

Show image from documents folder

I retrieve an image I have saved at the documents directory like this:
//set background image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"/myFolder/back.png"];
backgroundImage.image =[UIImage imageWithContentsOfFile:imagePath];
But it is not shown :(
What am I doing wrong?
PD:Yes, it has been saved with the same path and filename (no upper/lower case differences) and yes, I tried opening the image at that path and it works.
Thanks in advance!
It was being done in the wrong place. I was doing it at init instead of viewDidLoad.

NSDirectoryEnumerator And Subfolders

I have an iPhone app that searches a folder, collates an an array of all the audio files, and lets them be played back. The problem is that if there is a subfolder within the folder I am searching, it will just skip over it/not go into its contents.
My code is as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];
NSString *pname;
while (pname = [direnum nextObject])
{
[musicArray addObject:[pname stringByDeletingPathExtension]];
}
What I want to do is continue to search its subfolders, how would I go about doing that?
It does it automatically. From the documentation:
An enumeration is recursive, including the files of all subdirectories, and crosses device boundaries. An enumeration does not resolve symbolic links, or attempt to traverse symbolic links that point to directories.