I am having an issue copying things from the bundle to the Documents directory on first run. It works fine in the Simulator but doesn't work on the phone. Can anyone shed some light on my issue?
I am checking to make sure I only copy over .png files that have filenames of a certain length and contain dashes (xxxx-xx-xx.png) and that code works fine as I have tested it with NSLog messages. It is the actual copying code that seems to be faulty.
// get all the files in the directory
NSArray *dataFiles = [fm contentsOfDirectoryAtPath:documentsDirectory error:&dataError];
int fileCount = [dataFiles count];
Probably something simple- any help would be appreciated.
(BTW the space in the NSDocumentDirectory was inserted by the forum software and does not appear in my code)
First a note, use [code] [ /code] tags when posting code. It preserves indentation and makes it easier to read.
Second, what do you mean when you say it doesn't work on the device? Does it give an error/crash? If not, what happens when it is executed? I see you put a few NSLog lines there, what is the output in the console? Does it write out "Documents Directory is Empty"? Put maybe one in the for loop, there's a chance fileCount2 is zero so that's why nothing is happening. Also note that the filesystem on the device is case sensitive, and on the simulator it isn't, so the names have to match exactly.
I am having an issue copying things from the bundle to the Documents directory on first run. It works fine in the Simulator but doesn't work on the phone. Can anyone shed some light on my issue?
I am checking to make sure I only copy over .png files that have filenames of a certain length and contain dashes (xxxx-xx-xx.png) and that code works fine as I have tested it with NSLog messages. It is the actual copying code that seems to be faulty.
// get all the files in the directory
NSArray *dataFiles = [fm contentsOfDirectoryAtPath:documentsDirectory error:&dataError];
int fileCount = [dataFiles count];
Probably something simple- any help would be appreciated.
(BTW the space in the NSDocumentDirectory was inserted by the forum software and does not appear in my code)
Thanks-
Keith
I think the problem is the way you are building the starting path to the directory in your bundle. Don't use NSHomeDirectory and append your app name to get to the bundle.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
I used the code you suggested to build the path but it still isn't moving the files from the bundle to the documents directory. I know that it is entering the loop and making it through the conditionals, the only thing that doesn't seem to work is the actual code for moving the files. Are there permission issues I should be aware of?
I would think that the code for moving stuff is pretty straightforward, but I am hung up on this right now.
I used the code you suggested to build the path but it still isn't moving the files from the bundle to the documents directory. I know that it is entering the loop and making it through the conditionals, the only thing that doesn't seem to work is the actual code for moving the files.
How do you know the files are not being copied? How did you check?
OK- Finally figured out the issue. Apparently you cannot moveItemAtPath when going from the Main Bundle to the Documents Directory- instead you have to copyItemAtPath in order for it to work.
OK- Finally figured out the issue. Apparently you cannot moveItemAtPath when going from the Main Bundle to the Documents Directory- instead you have to copyItemAtPath in order for it to work.
Thanks for everyone who helped out.
Oh yeah.
I didn't notice that you were using moveItemAtPath.
On the iPhone the app bundle is read-only. Any attempt to change the contents of the app bundle will fail.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Not a problem Duncan. You are always spot on with stuff so I'll let this one slide.
I knew it was something stupid and easy - I just wasn't getting it. So easy to overlook too, especially since the code IS correct except in this context. Thanks again for your help.
As a minor side note, you really shouldn't be using stringByAppendingString: to form paths; that's what stringByAppendingPathComponent: is for. It's both cleaner (less code to write/read) and safer (for example, it makes sure there are no missing path separators, and no doubled up path separators).
Thanks for the hint on the code tags. I was wondering why it wasn't working for me. Turns out it was an ID-10-T error on my part.
A suggestion for the future: If you're getting unexpected results, and using a system function that uses a pointer to an NSError object, pass in a pointer instead of nil, and check the result. The error you were getting would probably have told you exactly what was going wrong.
Something like this:
Code:
NSError* result = nil;
[fm moveItemAtPath:myPath toPath:myNewPath error: &result];
if (result != nil)
NSLog(@"moveItemAtPath:myPath returned error %@", result);
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Thanks dljeffery- I have made this modification of my code as per your suggestion and I will make sure to use it from now on. One of the things about starting programming with a new language is trying to figure out how to get things to work - and sometimes the ways we stumble upon aren't as elegant or the best ways to do them. I have quite a notebook of code suggestions now and this one is one of them.
Thanks-
Keith
Quote:
Originally Posted by dljeffery
As a minor side note, you really shouldn't be using stringByAppendingString: to form paths; that's what stringByAppendingPathComponent: is for. It's both cleaner (less code to write/read) and safer (for example, it makes sure there are no missing path separators, and no doubled up path separators).
Very nice Duncan. I will use this in the future as well. I have been trying to figure out how to get meaningful errors from my coding and this helps immensely.
Thanks again-
Keith
Quote:
Originally Posted by Duncan C
A suggestion for the future: If you're getting unexpected results, and using a system function that uses a pointer to an NSError object, pass in a pointer instead of nil, and check the result. The error you were getting would probably have told you exactly what was going wrong.
Something like this:
Code:
NSError* result = nil;
[fm moveItemAtPath:myPath toPath:myNewPath error: &result];
if (result != nil)
NSLog(@"moveItemAtPath:myPath returned error %@", result);