objective-c - Cocoa/Obj-C - 将文件拖到应用程序图标时打开文件

标签 objective-c cocoa xcode macos nsdocument

目前我的应用程序界面上有一个允许打开文件的按钮,这是我的打开代码:

在我的应用程序中:

- (IBAction)selectFile:(id)sender;

在我的应用程序中:

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (IBAction)selectFile:(id)sender {

    NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
    NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil];

    NSInteger result  = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];

    if(result == NSOKButton){

        NSString * input =  [openPanel filename];

如何编辑我的代码以允许使用应用程序图标拖放打开?
注意:我编辑了 .plist 文件并为“xml”添加了一行,但它改变了任何东西,当我的文件放在图标上时出现错误。
注意 2:我将“文件 -> 打开...”链接到 selectFile:引用我的代码
注3:我的应用不是基于文档的应用


感谢您的帮助!
米斯基亚

最佳答案

首先将适当的扩展名添加到 .plist 文件中的 CFBundleDocumentTypes。

接下来实现以下委托(delegate):
- 应用程序:打开文件:(一个文件被丢弃)
- 应用程序:openFiles:(删除多个文件)

引用:
NSApplicationDelegate Protocol Reference

对评论的回应:

一步一步的例子,希望它能让一切都清楚:)

添加到.plist文件:

 <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeExtensions</key>
                <array>
                    <string>xml</string>
                </array>
                <key>CFBundleTypeIconFile</key>
                <string>application.icns</string>
                <key>CFBundleTypeMIMETypes</key>
                <array>
                    <string>text/xml</string>
                </array>
                <key>CFBundleTypeName</key>
                <string>XML File</string>
                <key>CFBundleTypeRole</key>
                <string>Viewer</string>
                <key>LSIsAppleDefaultForType</key>
                <true/>
            </dict>
        </array>

添加到...AppDelegate.h

- (BOOL)processFile:(NSString *)file;
- (IBAction)openFileManually:(id)sender;

添加到...AppDelegate.m

- (IBAction)openFileManually:(id)sender;
{
    NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
    NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil];
    NSInteger result  = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];
    if(result == NSOKButton){
        [self processFile:[openPanel filename]];
    }
}

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
    return [self processFile:filename];
}

- (BOOL)processFile:(NSString *)file
{
    NSLog(@"The following file has been dropped or selected: %@",file);
    // Process file here
    return  YES; // Return YES when file processed succesfull, else return NO.
}

关于objective-c - Cocoa/Obj-C - 将文件拖到应用程序图标时打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5331774/

相关文章:

ios - 为什么 subview Controller 的 View 和 subview 不调整大小?

ios - 如何停止 uisearch 即时搜索?

objective-c - "LSSharedFileList.h"在哪里?

swift - 修复插入 "as! PFUserResultBlock"

ios - 无法在 iphone 设备上调试我的 xamarin 表单应用程序

objective-c - 检测 UITextView 中的换行符

objective-c - TableView 未根据绑定(bind)更新

cocoa - NSNumberFormatter 删除尾随零

ios - 如何在 Xcode 的 iOS UI 测试中选择一个选择器 View 项?

ios - dispatch_after 在 ios 上循环不工作