iCloud & NSFileWrapper: Showing as 2 different files in Settings
Asked Answered
A

3

9

I have an app that uses a UIDocument that is NSFileWrapper based. My file wrapper is a directory named "XXX.cp", with two sub-files "photo.data" and "photo.metadata". It seems to save and load documents fine, however when I go to Settings\Manage Storage\Unknown the sub-files are listed separately:

Settings

I was expecting it to show "XXX.cp" instead of these two sub-files. I think I have the document UTI set up and exported properly:

UTI UTI Export

And I think I am creating the file wrappers correctly (especially since it reads/writes fine):

- (void)encodeObject:(id<NSCoding>)object toWrappers:(NSMutableDictionary *)wrappers preferredFilename:(NSString *)preferredFilename {    
    @autoreleasepool {                
        NSMutableData * data = [NSMutableData data];    
        NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];    
        [archiver encodeObject:object forKey:@"data"];    
        [archiver finishEncoding];   
        NSFileWrapper * wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];    
        [wrappers setObject:wrapper forKey:preferredFilename];    
    }    
}        

- (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {        
    if (self.captionedPhotoMetadata == nil || self.captionedPhoto == nil) {    
        *outError = [[NSError alloc] initWithDomain:CaptionedPhotoErrorDomain code:CaptionedPhotoInvalidDocument userInfo:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Invalid document!", @""), NSLocalizedDescriptionKey, nil]];    
        return nil;        
    }    
    NSMutableDictionary * wrappers = [NSMutableDictionary dictionary];    
    [self encodeObject:self.captionedPhotoMetadata toWrappers:wrappers preferredFilename:METADATA_FILENAME];    
    [self encodeObject:self.captionedPhoto toWrappers:wrappers preferredFilename:DATA_FILENAME];       
    NSFileWrapper * fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers];    
    return fileWrapper;        
}

But still no cigar. Anyone know what the problem is? Thanks!

Atory answered 21/4, 2012 at 15:26 Comment(0)
A
5

Figured this out. The problem was you need to have a filename extension of 3 letters. Mine had only 2 letters: ".cp". I switched it to ".cap" and it worked :P

Atory answered 23/4, 2012 at 13:25 Comment(4)
I've had this issue too and I have 3 letters for my file extension. I use the extension .tfx for my directory based file wrapper. I recently decided to put my wrapper files in a separate folder in my ubiquitous container I call SyncDocuments. That way the individual files don't show up for the user. But this is what I really want as my UIDocument subclass files are not meant for users to interact with directly.Odontograph
I have exactly the same issue, but my file extension has three letters (and an icon). I think this is some fishy setting in the exported UTI. My hunch is, that there's some kind of caching going on in the iCloud and that changing the extension from .cp to .cap fixed something that was broken.Salpiglossis
Hey Ray love your tutorials, but I am still having an issue. I can get my wrappers to show as package but then my nsmetadataquery doesn't see them. Not event if I search all documents with a *. My first document type works, but my images one does notAmarillo
One more thing, I am using xcode 5, on the simulator for iOS 7 it is able to see the package with extension, on device it sees the package contents.Amarillo
N
1

I didn't change my UTI to be three characters. It remains 11 characters. At first I was seeing the same issue Ray is reporting. I looked in the documentation more and found in "iOS Apps Programming Guide" - "App-Related Resources" under the paragraph of "Declaring Your App's Supported Document Types" that "For each document type you must provide the following information at a minimum:

  • A name
  • An icon
  • The file types

I had provided a name and the file types, but not an icon. So I whipped up the most awesome little document icon I could in 22 seconds, added it to my project, added it to the Target's Document Types, and on my iPad, Settings, iCloud, Manage Storage, MyApp, I only see the file wrapper name, NSMetadataItemDisplayNameKey style!

Nightspot answered 16/6, 2012 at 1:8 Comment(0)
S
0

I had the same issue, but my extension had three chars and I also had an icon. However, after reading this comment, I tried deleting the LSItemContentTypes in the CFBundleDocumentTypes section ("Types: com.razeware.captionedPhoto" in the second image under "document types") and it worked.

I also noticed that the WWDC demo "UIDocument and iCloud" has no LSItemContentType set as well. Seems like a huge bug to me, as the docs clearly state there should be a document type.

Salpiglossis answered 20/2, 2013 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.