Obj-C:应用程序:openFiles :(删除多个文件)没有收到所有文件。错误?

时间:2015-12-02 09:31:14

标签: objective-c cocoa drag-and-drop info.plist

我找到的最近的一个是:https://stackoverflow.com/questions/4778932/cocoa-app-not-receiving-all-dropped-files-in-applicationopenfiles 但它还没有得到回答。

我的问题是这个, 所以,让我说我在Dock应用程序上删除了4个文件:

  • TEST.XLS
  • TEST.RTF
  • test.jpg
  • 检验.pdf


如果我去调试我的程序,它会显示:

Debug log

正如您所见,.jpg文件不在那里。 这也适用于:.py,其他图像类型,.txt文件 我发现如果我只从该组中删除文件(.py .jpg .txt),那么它会识别所有这些文件。

我的Info.plist看起来像这样:

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plistversion="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>AllFiles</string>
<key>CFBundleTypeIconFile</key>
<string>application.icns</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.item</string>
</array>
</dict>
</array>

我不知道错误在哪里,也许这是一个错误?或者我的info.plist配置错误了吗?

我希望有人可以帮助我,并提前感谢你们!

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,这就是我如何解决它:

@property (strong) NSMutableArray *openFilesList;
@property (strong) NSTimer *openFilesTimer;

...

@synthesize openFilesList;
@synthesize openFilesTimer;

...

-(void)init {
    openFilesList = [[NSMutableArray alloc] init];
    openFilesTimer = nil;
}

-(void)application:(NSApplication *)sender openFiles:(NSArray *)filenames {
    if (openFilesTimer) {
        [openFilesTimer invalidate];
    }
    [openFilesList addObjectsFromArray:filenames];
    openFilesTimer = [NSTimer scheduledTimerWithTimeInterval:0.2f
                                                      target:self
                                                    selector:@selector(processOpenFiles)
                                                    userInfo:nil
                                                     repeats:NO];
}

-(void)processOpenFiles {
    NSArray *filenames = [openFilesList copy];
    openFilesTimer = nil;
    [openFilesList removeAllObjects];

    // Do your processing with filenames
}

答案 1 :(得分:0)

像这样

在info.plist中设置
<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>*</string>
            </array>
            <key>CFBundleTypeName</key>
            <string>Files</string>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>public.data</string>
            </array>
        </dict>
    </array>

并在AppDelegate.m中使用此方法

-(void)application:(NSApplication *)sender openFiles:(NSArray *)fileList{
 // use fileList array 
}

-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename{
    return YES;
}