Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jnoxx

macrumors 65816
Original poster
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
Hai Guys,

I'm having a bit of an issue here..
I'm trying to implement file sharing via iTunes in 2 projects.
One goes like this -> you drag an picture in iTunes, and on every App resume you will copy them to a special made folder in the NSLibrary, because apparently Apple started rejecting apps for writing to the NSDocument folder.

In another project, I want to be able to save something from the iPhone itself to the file sharing, so you can take it off via iTunes..
But since this has changes (the way to do it), I can't really find good tutorials or so.
ATM I have something like this in the App resume:

Code:
    NSMutableArray *retval = [NSMutableArray array];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *publicDocumentsDir = [paths objectAtIndex:0];   
    
    NSError *error;
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:publicDocumentsDir error:&error];
    if (files == nil) {
        NSLog(@"Error reading contents of documents directory: %@", [error localizedDescription]);
    } else {
        for (NSString *file in files) {
            if ([file.pathExtension compare:@"png" options:NSCaseInsensitiveSearch] == NSOrderedSame) {        
                NSString *fullPath = [publicDocumentsDir stringByAppendingPathComponent:file];
                [retval addObject:fullPath];
            }
        }
    }    

    NSError *error2 = nil;
    NSArray *libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [libPath objectAtIndex:0];
    NSString *iconpath = [libraryDirectory stringByAppendingFormat:@"/test.png"];
    
   NSURL *iconURL = [NSURL fileURLWithPath:iconpath];
    NSLog(@"iconURL: %@", iconURL);
    //NSArray *iconFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:iconPath error:nil];
    
    for (int i = 0; i < [retval count]; i++) {
        NSString *file = [retval objectAtIndex:i];
        NSURL *url = [NSURL fileURLWithPath:file];
        if ([file.pathExtension compare:@"png" options:NSCaseInsensitiveSearch] == NSOrderedSame) { 
            BOOL succes = [[NSFileManager defaultManager] copyItemAtURL:url toURL:iconURL error:&error2]; 
            if (succes) {
                NSLog(@"it worked, copied file");
            } else {
                NSLog(@"error2: %@", [error2 localizedDescription]);
            }
        }
    }

And for now i'm coming up with an blank image..
For the second project, with the other way around i'm a bit blind atm, hope someone can shed a light at it.

Greetings, Noxx
 
I know in the Doc's they push for the Documents Folder only being used for User Files. If your using iTunes sharing would have thought those flles belong to the user and not going to be an issue.

There seems to some discussion here on stackoverflow.

Edit: corrected link.
Sorry for the cut and distracted paste.
 
Last edited:
I know in the Doc's they push for the Documents Folder only being used for User Files. If your using iTunes sharing would have thought those flles belong to the user and not going to be an issue.

There seems to some discussion here on stackoverflow.

Why are you sending me spam?
http://www.interloc-lockers.com.au/?gclid=CI2i3MvD_q8CFcgopAod7XCAEA

What is it with that link.
The issue why they started rejecting, is something about iCloud support + Documents folder, but I haven't figured it out yet ;(
 
There is some issue with Apple regarding backups to iCloud. I think they don't want developers to allow backup of files that can be recovered. Meaning files that are downloaded from the net and cached on the device. User created files though should be backed up. I have heard something about apps being rejected if they allow backup of many files that can be recovered.

Regarding exporting files to iTunes, it's just a matter of copying them to the Documents folder and enabling iTunes file sharing.

Importing is a little different. If you want you can just leave the files in the Documents folder and let your user choose a file to open from there. That's the simplest. The user has control then of what files are there and the user can delete the files from iTunes and of course add more at any time. If you want to copy a file into a private folder then there's a problem. Your app can't tell exactly when the file has been completely written to the Documents folder. Before iOS 5.0 this was clear because the app was always suspended while the file was copied. Since 5.0 apps are not suspended when files are copied to their Documents folder and a big file can take 5 or more seconds to be copied in by iTunes. There's been discussion of this in the Core sub forum at Apple's iOS forum. There's some code there that I use that waits for a kqueue notification and once it receives it checks the file size of files in the Documents folder every second or so after that. When the file size doesn't change it then assumes the file has been completely written and imports the file. Unfortunately the kqueue notification comes when the file starts being written, not when it finishes being written.
 
Hey, that's an indept explenation, I'm trying to figure it all out, and i'll make a blog post about it, and ofcourse, will head back with some links.
But for now, thanks alot :)
 
One more question, I currently implemented fully functioning copying in 2 projects, but in both the plists I use INSIDE the project, appear in the iTunes file sharing.. Why? I have never selected those to be in the iTunes file sharing (documents folder), and why arent all the other images for example in there? (this happens in both projects).
Greetings, Noxx
 
It's not obvious to me why that would happen. Did you try debugging this? Print out the files that are copied from your bundle to the Documents folder.
 
Hey Phoney, just try it out :)
Enable file sharing in a project, add an plist, and build, it will show up in the iTunes File sharing option.. I tried with a clean project yesterday, I find it very strange..

Greetings, Noxx
 
Couple comments on that code:

You should use the path methods rather than basic string methods to manipulate paths. So use lastPathComponent and stringByAppendingPathComponenet rather than componentsSeparatedByString and stringByAppendingFormat to manipulate paths. It will shorten your code and make it easier to read.

You should use fast iteration when possible and not objectAtIndex: to iterate over the contents of an array.

Your code ignores the possibility of a file being uploaded more than one time to the Documents folder. What happens when the user uploads a new version of an existing file?

You ignore the issue of when to do the checkOrCopyImages.

OK should always be capitalized. Not Ok.
 
Couple comments on that code:

You should use the path methods rather than basic string methods to manipulate paths. So use lastPathComponent and stringByAppendingPathComponenet rather than componentsSeparatedByString and stringByAppendingFormat to manipulate paths. It will shorten your code and make it easier to read.

You should use fast iteration when possible and not objectAtIndex: to iterate over the contents of an array.

Your code ignores the possibility of a file being uploaded more than one time to the Documents folder. What happens when the user uploads a new version of an existing file?

You ignore the issue of when to do the checkOrCopyImages.

OK should always be capitalized. Not Ok.

:eek:
Thanks for the completely valid input ^_- I will change it Phoney.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.