如何在XCode 7中关联Mac OS应用程序的现有文件类型?

时间:2016-02-28 05:54:47

标签: xcode macos cocoa file-association

我正在为Xcode 7中的jpeg图片制作一个简单的查看器,并尝试将Jpeg文件类型与我的应用程序相关联。这是Swift中OS X的Cocoa应用程序,它使用故事板,而不是基于文档的应用程序。

我在网上找到的教程建议浏览“信息”选项卡,在那里添加新的文档类型。现在,这些内容开始与教程中的内容不同:在它们中只有3个字段可以填写(名称,类型,图标),但我还有更多。我试着尝试一下,这就是我把它放在我得到的字段中:

  • 名称:JPEG图像
  • 类:将其留空
  • 扩展程序:.jpg,.jpeg,.JPEG,.JPG
  • 图标:将其留空
  • 标识符:public.jpeg
  • 角色:查看器
  • Mime类型:image / jpeg
  • “文档作为包分发”默认情况下部分检查;我只是原样离开了。
  • 未触及其他文档类型属性。

因此,当我二次点击Jpeg文件以及其他已安装的应用程序时,我将我的应用程序显示在“打开方式”的列表中,但是一旦我尝试以这种方式打开它,我会得到一个弹出窗口文件“picture.jpg”无法打开。 MyApp无法以“JPEG图像”格式打开文件

我做错了什么?

1 个答案:

答案 0 :(得分:0)

Class类是必须的,你必须有相应的实现。

尝试:

Class: $(PRODUCT_MODULE_NAME).Document

然后添加Document.swift

import Cocoa

class Document : NSDocument
{

    override init() {
        super.init()
        // Add your subclass-specific initialization here.
    }

    override class func autosavesInPlace() -> Bool {
        return true
    }

    override func makeWindowControllers() {
        // Returns the Storyboard that contains your Document window.
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        let windowController = storyboard.instantiateController(withIdentifier: "Document Window Controller") as! NSWindowController
        self.addWindowController(windowController)
    }

    override func data(ofType typeName: String) throws -> Data {
        // Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil.
        // You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
    }

    override func read(from data: Data, ofType typeName: String) throws {
        // Insert code here to read your document from the given data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning false.
        // You can also choose to override readFromFileWrapper:ofType:error: or readFromURL:ofType:error: instead.
        // If you override either of these, you should also override -isEntireFileLoaded to return false if the contents are lazily loaded.
        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
    }

}

^根据需要进行修改。

此外,您指定了角色" Viewer",表示您可以使用空格键打开它,而不是双击(打开) - 这是角色"编辑"正确?

相关问题