使用NSSavePanel将文件从捆绑包保存/复制到桌面 [英] Save/Copy a file from Bundle to Desktop using NSSavePanel

查看:128
本文介绍了使用NSSavePanel将文件从捆绑包保存/复制到桌面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个macOS应用,该应用在其Bundle目录中随附了一些.zip文件.

I’m creating a macOS app which ships with some .zip files within its Bundle directory.

用户应该能够将这些文件从我的应用程序保存到自定义目录.

Users should be able to save these files from my app to a custom directory.

我找到了NSSavePanel,并认为这是正确的方法-到目前为止,我就是这样:

I found NSSavePanel and thought it is the right approach — that’s what I have so far:

@IBAction func buttonSaveFiles(_ sender: Any) {

    let savePanel = NSSavePanel()

    let bundleFile = Bundle.main.resourcePath!.appending("/MyCustom.zip")

    let targetPath = NSHomeDirectory()
    savePanel.directoryURL = URL(fileURLWithPath: targetPath.appending("/Desktop")) 
    // Is appeding 'Desktop' a good solution in terms of localisation?

    savePanel.message = "My custom message."
    savePanel.nameFieldStringValue = "MyFile"
    savePanel.showsHiddenFiles = false
    savePanel.showsTagField = false
    savePanel.canCreateDirectories = true
    savePanel.allowsOtherFileTypes = false
    savePanel.isExtensionHidden = true

    savePanel.beginSheetModal(for: self.view.window!, completionHandler: {_ in })

}

我找不到如何将bundleFile移交给savePanel的方法.

I couldn’t find out how to 'hand over' the bundleFile to the savePanel.

所以我的主要问题是:如何将文件从应用捆绑包保存/复制到自定义目录?

So my main question is: How can I save/copy a file from the app bundle to a custom directory?

取决于NSSavePanel的其他问题:1)似乎默认情况下未进行本地化(我的Xcode方案设置为德语,但面板显示为英语),我是否必须自己定制? 2)有没有一种方法可以默认显示展开的面板?

Additional questions depending NSSavePanel: 1) It seems that it’s not localized by default (my Xcode scheme is set to German, but the panel appears in English), do I have to customize that by myself? 2) Is there a way to present the panel expanded by default?

推荐答案

您应该使用Bundle.main.url获取现有的文件URL,然后使用面板获取目标URL,然后复制该文件.面板对文件不做任何事情,它只是获取文件的URL.

You should use Bundle.main.url to get your existing file URL, then get the destination URL with the panel, then copy the file. The panel doesn't do anything to files, it just gets their URL.

示例:

// the panel is automatically displayed in the user's language if your project is localized
let savePanel = NSSavePanel()

let bundleFile = Bundle.main.url(forResource: "MyCustom", withExtension: "zip")!

// this is a preferred method to get the desktop URL
savePanel.directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!

savePanel.message = "My custom message."
savePanel.nameFieldStringValue = "MyFile"
savePanel.showsHiddenFiles = false
savePanel.showsTagField = false
savePanel.canCreateDirectories = true
savePanel.allowsOtherFileTypes = false
savePanel.isExtensionHidden = true

if let url = savePanel.url, savePanel.runModal() == NSFileHandlingPanelOKButton {
    print("Now copying", bundleFile.path, "to", url.path)
    // Do the actual copy:
    do {
        try FileManager().copyItem(at: bundleFile, to: url)
    } catch {
        print(error.localizedDescription)
    }
} else {
    print("canceled")
}

此外,请注意,正在展开或未展开的面板是用户的选择,您不能从应用程序中强制它.

Also, note that the panel being expanded or not is a user selection, you can't force it from your app.

这篇关于使用NSSavePanel将文件从捆绑包保存/复制到桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆