【发布时间】:2018-03-18 23:54:23
【问题描述】:
所以我让它工作了,然后我调整了一些东西:doc controller - subclassed as
class DocumentController : NSDocumentController {
override func typeForContents(of url: URL) throws -> String {
return "Document"
}
}
以及解析 .webloc 文件的 URL 助手:
extension URL {
var webloc : URL? {
get {
do {
let data = try Data.init(contentsOf: self) as Data
let dict = try! PropertyListSerialization.propertyList(from:data, options: [], format: nil) as! [String:Any]
let urlString = dict["URL"] as! String
return URL.init(string: urlString)
}
catch
{
return nil
}
}
}
}
当我在对打开与生成文件文档问题进行分类时丢失了我的测试应用程序的 Finder 菜单“打开方式”条目 - 见下文。我确实有一个应用程序委托 openFile: as
func application(_ sender: NSApplication, openFile: String) -> Bool {
let urlString = (openFile.hasPrefix("file://") ? openFile : "file://" + openFile)
let fileURL = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)!
return self.doOpenFile(fileURL: fileURL)
}
func doOpenFile(fileURL: URL) -> Bool {
let dc = NSDocumentController.shared()
var status : Bool = false
var itemURL = fileURL
// Resolve alias, container webloc bookmark before target bookmarking
if let original = (itemURL as NSURL).resolvedFinderAlias() { itemURL = original }
if itemURL.absoluteString.hasSuffix("webloc") {
if isSandboxed() != storeBookmark(url: itemURL as URL) {
Swift.print("Yoink, unable to bookmark (\(itemURL))")
}
if let webURL = itemURL.webloc {
itemURL = webURL
}
}
else
{
if isSandboxed() != storeBookmark(url: itemURL as URL) {
Swift.print("Yoink, unable to bookmark (\(itemURL))")
return false
}
}
dc.openDocument(withContentsOf: itemURL, display: true, completionHandler: { (nextURL, wasOpen, error) in
if error != nil {
NSApp.presentError(error!)
Swift.print("Yoink, unable to open doc for (\(String(describing: nextURL)))")
status = false
}
else
{
let newWindow = nextURL?.windowControllers.first?.window
(newWindow?.contentView?.subviews.first as! MyWebView).next(url: itemURL)
newWindow?.offsetFromKeyWindow()
status = true
}
})
return status
}
当我试图打开一个“webloc”网址而不是为它创建一个文档时,我的视图控制器收到了一个错误 (presentError:) - 微妙的区别?
无论如何,现在我的菜单条目在 Finder 中消失了,所以我正在寻找一个清单来验证我是否 [仍然] 设置正确,我的应用程序的单个文档 Info.plist 显示为(相关的文档)为:
我什么时候失踪了!?这项工作使用了其他人的 re: 解析 Finder alias、沙盒支持和 webloc 内容解析。
我试图解决一个问题,即当我进行“一些”更改并丢失 Finder 的“打开方式”条目时,我的 -URL.webloc() 函数没有解决搜索字符串编码的文件名 - webloc应用程序。
因此,为了我和其他人的利益,我们需要一份检查清单(食谱)来运行。这个应用是一个测试迷你浏览器,使用带有 next(url:) 函数的 WKWebView 来加载下一个 url。
【问题讨论】:
-
请注意,如果您需要创建一个没有方案“file://”的文件 URL,您只需要使用 URL 初始化器
URL(fileURLWithPath:) -
你需要了解URL属性
absoluteString和path的区别 -
是的,后者不包含架构文件://,但这是问题所在吗?在 webloc 检查中,两者都可以,因为我认为测试没有区别。这引入了另一个我将单独问的问题 - 沙盒 webloc url,以便可以读取它们的内容。
-
有时启动服务数据库(控制查找器菜单)会损坏。尝试重建它。删除所有旧副本也会有所帮助。使用访客帐户或在另一台新计算机上进行测试。
标签: swift macos sandbox finder