file exist in Document folder but not for Xcode - iphone

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...

Related

save audio file to NSURL

I want to save audio file generated by TTS SDK.
I am not sure what is correct way to do it with NSURL path.
This is the code, but result says NO.
If I don't try saving audio file, MyAcaTTS works fine.
NSString *FileNamePath = [[NSBundle mainBundle] pathForResource:#"testAudio" ofType:#"aiff"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:FileNamePath];
NSURL *url=[[NSURL alloc]initWithString:appSettingsPath];
BOOL result = [MyAcaTTS_ startSpeakingString:#"testing" toURL:url];
Document of Acapela iPhone SDK.
6.2.3.startSpeakingString:toURL:
Synopsis
(BOOL)startSpeakingString:(NSString *)string toURL:url;
Description
Begins synthesizing string into a sound (AIFF) file.
When synthesis of string finishes normally or is stopped, the message
speechSynthesizer:didFinishSpeaking: is sent to the delegate. Parameters
string
Text to synthesize. When nil or empty, no synthesis occurs.
url
Filesystem location of the output sound file. Return value
YES when synthesis starts successfully, NO otherwise.
http://www.ecometrixem.com/cms-assets/documents/44729-919017.acapela-for-iphone.pdf
There are two things for you to consider in your code:
The line
NSString *FileNamePath = [[NSBundle mainBundle] pathForResource:#"testAudio" ofType:#"aiff"];
is not required since it returns full path of the file while you need only the last part of it: "testAudio.aiff"
You construct URL object with constructor accepting strings with valid protocol prefix, like "http://" or "ftp://" while your need another constructor named initFileURLWithPath: instead.
So with all the above your code may look like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:#"testAudio.aiff"];
NSURL *url=[[NSURL alloc]initFileURLWithPath:appSettingsPath];
BOOL result = [MyAcaTTS_ startSpeakingString:#"testing" toURL:url];

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];

Saving NSMutableArray to iPhone device

In the Simulator I can save an NSMutableArray to a file and read it back with the following code:
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:#"RiskValues"]){ // If file exists open into table
NSLog(#"Risk Values File Exists");
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:#"RiskValues", documentsDirectory];
gRiskValues = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
gRiskValuesAlreadyInitialised = YES;
} else {
NSLog(#"Can't find RiskValues file, so initialising gRiskValues table");
Do something else .......
}
This doesn't work on the device. I have tried to locate the file using the following but it still doesn't work:
NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent#"RiskValues"];
What am I doing wrong?
Great answers from everyone. I have resolved the file path and existence issues at a stroke. Many, many thanks.
You have to provide absolute path here:
if ([fileManager fileExistsAtPath:#"RiskValues"])
So it must look like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent:#"RiskValues"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: fullFileName]){ // If file exists open into table
NSLog(#"Risk Values File Exists");
gRiskValues = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
gRiskValuesAlreadyInitialised = YES;
} else {
NSLog(#"Can't find RiskValues file, so initialising gRiskValues table");
Do something else .......
}
NSString *fullFileName = [NSString stringWithFormat:#"RiskValues", documentsDirectory];
this line, you're not creating your full path string right. what you should do is
NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent:#"RiskValues"];
also this check
if ([fileManager fileExistsAtPath:#"RiskValues"])
Will never pass on iOS as it is not a full path to any place you are allowed to write at in your sandbox. I suppose it works on the simulator because on the mac it's looking up relatively to the HD root (or something, not sure how the mac file system works :) ), but on the iOS you're going to have to give it a path to a file/directory in your documents (maybe by appending #"RiskValues" to it or whatever)
1) [NSString stringWithFormat:#"RiskValues", documentsDirectory] is just #"RiskValues". So this name points to file in application's directory.
2) [fileManager fileExistsAtPath:#"RiskValues"] searches for file in application directory. It's available for read/write in simulator (it's in your computer file system after all) but it's read-only on device.
BTW (NSFileManager Class Reference)
Attempting to predicate behavior based
on the current state of the file
system or a particular file on the
file system is not recommended. Doing
so can cause odd behavior in the case
of file system race conditions. It's
far better to attempt an operation
(such as loading a file or creating a
directory), check for errors, and
handle any error gracefully than it is
to try to figure out ahead of time
whether the operation will succeed.
Solution:
1) Do not check file presence. Just try to make dictionary with initWithContentsOfFile:encoding:error:
2) You want it to be in documents directory so construct path like this
[documentsDirectory stringByAppendingPathComponent:#"RiskValues"];

how to access a folder in documents using iphone

i can access .txt file from documents folder but how to access a folder content lets say documents/A
inside A i have ->a.html, update.cfg
now why i cant access update.cfg??
i am getting null value for zipPath
i tried this but no luck
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *aDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"A"];
NSString *zipPath = [[NSBundle mainBundle] pathForResource:#"update" ofType:#"cfg" inDirectory:aDirectory];
still zipPath=NULL??
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *aDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"A"];
My approach to get to the documentsfolder is a little bit different. (I hope you mean the Documents folder which every application has, not one created by yourself in the mainbundle.^^) I do it like this:
NSString *directoryPath = [[NSHomeDirectory() stringByAppendingPathComponent: #"Documents"] stringByAppendingPathComponent: #"A"];
This is the path to your directory called A in the documents folder. If you know the filename, than you can use another "stringByAppendingPathComponent". ;-) I hope it helps. Else ask again. :-D
after messing up i found this way to acces the file from folder
i got it
NSString *fileName = [NSString stringWithFormat:#"%#/update.cfg",
aDirectory];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];
thanks

Check if file exist or not?

i am new to iPhone programming.
What is the proper way to check that whether a file is exist or not?
BOOL isDirectory = NO;
if ( [[NSFileManager defaultManager]
fileExistsAtPath:path
isDirectory: &isDirectory ]) {
// file already exists
} else {
// file does not yet exist
}
To put it in more detail:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]){
//Do something...
}
You can append the actual file name to "documentsDirectory" like this: [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.jpg", spidermanpic]].
The isDirectory option in the answer above is used to check if the path is a directory or a file. Please keep in mind that it is a pointer. It wont work without the "&".