Document Type Icon not showing on custom file MacOS

自古美人都是妖i 提交于 2021-02-08 20:54:12

问题


My application needs to have custom document support, but I did not start out with a Document-template in xcode. I did manage to properly load and save the data by following among other things this tutorial: https://www.brandpending.com/2016/02/21/opening-and-saving-custom-document-types-from-a-swift-cocoa-application/. One problem though, is that my custom icon is not showing on my file! it is still a blank page-icon :(.

What Am I doing wrong?

Here is my setup:

my HSDocument code:

-(BOOL)readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError
{
    NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver   unarchiveObjectWithFile:[url path]];
    self.archive = myDictionary;

    return YES;
}

-(BOOL)writeToURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError
{
    [NSKeyedArchiver archiveRootObject:self.archive toFile:[url path]];

  return YES;
}

And here is how I load/save my data to the file: (I don't want it to be triggered with the normal menu-items)

- (void)onLoadFile
{
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setCanChooseFiles:YES];
    [panel setCanChooseDirectories:YES];
    [panel makeKeyAndOrderFront:nil];
    [panel setLevel:NSStatusWindowLevel];
    [panel setCanCreateDirectories:NO];


    [[self.preferencesWindow window] setLevel:kCGNormalWindowLevel];

    [panel setFrameOrigin:CGPointZero];

    [panel beginWithCompletionHandler:^(NSInteger result)
    {
        if (result == NSFileHandlingPanelOKButton)
        {
            for (NSURL *url in [panel URLs])
            {
                NSError *error;
                HSDocument *document = [HSDocument new];

                [document readFromURL:url ofType:@"my-identifier" error:&error];
                [self setPresetsFromCollection:document.archive onStartup:NO];
            }
        }

        [[self.preferencesWindow window] setLevel:NSStatusWindowLevel];
    }];
}

- (void)onSaveFile
{
    NSSavePanel *panel = [NSSavePanel savePanel];
    [panel setMessage:@"Please select a location to save the file."];
    [panel setAllowsOtherFileTypes:YES];
    [panel setExtensionHidden:YES];
    [panel setTitle:@"Saving file..."]; // Window title
    [panel setCanCreateDirectories:YES];

    [[self.preferencesWindow window] setLevel:kCGNormalWindowLevel];

    [panel beginWithCompletionHandler:^(NSInteger result)
    {
        if (result == NSFileHandlingPanelOKButton)
        {
            NSURL* theUrl = [panel URL];
            theUrl = [theUrl URLByAppendingPathExtension:@"hej"];

            HSDocument *document = [HSDocument new];
            document.archive = [self collectPresetsToSave];
            NSError *error;
            [document writeToURL:theUrl ofType:@"my-identifier" error:&error];
        }

        [[self.preferencesWindow window] setLevel:NSStatusWindowLevel];
    }];
}

Thnx!


回答1:


Had the same problem in Mojave, Xcode 10.1.

What finally solved the problem was to use an .ICNS-file for the icon in Document Types. As long as I tried with PNG-files, these were never displayed in finder.



来源:https://stackoverflow.com/questions/42089382/document-type-icon-not-showing-on-custom-file-macos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!