NSDocument Directory / Itunes File sharing

Discussion in 'iOS Programming' started by jnoxx, May 13, 2012.

  1. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #1
    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
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Say what?
     
  3. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    Hey Dejo,

    I have read (trying to find the resource), that Apple is rejecting NEW applications for writing to the Documents Directory, it should be the Library or Caches Directory.. Anyways, any chance you know an answer to my question? ;p
     
  4. MattInOz, May 13, 2012
    Last edited: May 14, 2012

    MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #4
    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.
     
  5. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    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 ;(
     
  6. forum user macrumors regular

    Joined:
    Aug 28, 2008
    #6
  7. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #7
  8. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    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.
     
  9. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #9
    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 :)
     
  10. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #10
  11. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #11
    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
     
  12. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #12
    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.
     
  13. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #13
    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
     
  14. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #14
  15. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #15
    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.
     
  16. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #16
    :eek:
    Thanks for the completely valid input ^_- I will change it Phoney.
     

Share This Page