【发布时间】:2019-09-11 01:16:17
【问题描述】:
我是 iOS 的开发者。我目前正在制作一个加密钱包。加载keystore 文件类型时不支持问题。我将Info.list文件中的数据类型保存为public.data以扩大范围。我可以选择一个文件,但我在加载它时没有传递它。有什么问题?
DocumentBrowserViewController.swift
class DocumentBrowserViewController : UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
...
func presentDocument(at documentURL: URL) throws {
// Start accessing a security-scoped resource.
guard documentURL.startAccessingSecurityScopedResource() else {
throw IXError.fileAcessFailed
}
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let documentViewController = storyBoard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
documentViewController.document = Document(fileURL: documentURL)
present(documentViewController, animated: true, completion: nil)
Log.Debug(documentURL)
defer { documentURL.stopAccessingSecurityScopedResource() }
}
Document.swift
import UIKit
class Document: UIDocument {
var fileData: Data?
var filesText: ****.Wallet?
override func contents(forType typeName: String) throws -> Any {
Log.Debug("**********************************************************")
if typeName == "public.data" {
if let content = filesText {
let data = content
return data
} else {
return Data()
}
} else {
Log.Debug("**********************************************************")
return Data()
}
} // end func contents
override func load(fromContents contents: Any, ofType typeName: String?) throws {
if let fileType = typeName {
if fileType == "public.png" || fileType == "public.jpeg" { // .jpg not recognized
if let fileContents = contents as? Data {
fileData = fileContents
}
} else if fileType == "public.data" {
if let fileContents = contents as? ***.Keystore {
// filesText = String(data: fileContents, encoding: .utf8)
filesText = ***.Wallet.init(keystore: fileContents)
}
} else {
Log.Debug("**********************************************************")
print("File type unsupported.") // Keystore file here
}
} // end if let fileType = typeName
} // end func load
public var state: String {
switch documentState {
case .normal:
return "Normal"
case .closed:
return "Closed"
case .inConflict:
return "Conflict"
case .savingError:
return "Save Error"
case .editingDisabled:
return "Editing Disabled"
case .progressAvailable:
return "Progress Available"
default:
return "Unknown"
}
} // end public var state
} // end class Document
MainController.swift
document?.open(completionHandler: { (success) in
if success {
if self.document?.fileType == "public.png" || self.document?.fileType == "public.jpeg" {
// self.imageView.image = UIImage(data: (self.document?.fileData)!)
Log.Debug("#################################################")
print("\(String(describing: self.document?.fileData))")
Log.Debug("#################################################")
// #4.5 - If the UIDocument reports that it is a text file...
} else if self.document?.fileType == "public.data" {
// self.textView.text = self.document?.filesText!
Log.Debug("**********************************************************")
print("\(String(describing: self.document?.filesText))")
Log.Debug("**********************************************************")
}
Log.Debug("**********************************************************")
print("\(String(describing: self.document?.fileURL.lastPathComponent))")
Log.Debug("**********************************************************")
print("Document state: \((self.document?.state)!)")
Log.Debug("**********************************************************")
} else {
// Make sure to handle the failed import appropriately, e.g., by presenting an error message to the user.
}
})
Info.list
文件网址
file:///Users/******/Library/Developer/CoreSimulator/Devices/15169C47-DC68-4B14-B8E8-F75E4108AB7A/data/Containers/Data/Application/499E0805-A3AE-4312-B8B4 -55236CFC7BD8/文档/***/******
内容:
Document.swift load(fromContents:ofType:) [Line:69]
类型名称:
load(fromContents:ofType:) [Line:71] 可选(“dyn.ah62d4rv4ge8xeqbwtk0w43dbqru0qq30gu4xwsm3g23dg3vwha4xn3pdqmvdqsm0qe6xgqbuqu6ds2mwqmw0q2v2he”)
此文件未通过这两个条件中的任何一个。我的文件必须通过“public.data”条件。我做错了什么?
**************************************************** 编辑*************************
Info.list
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>images</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.image</string>
</array>
</dict>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>data</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.data</string>
</array>
</dict>
</array>
【问题讨论】:
-
dyn.ah62d4rv4ge8xeqbwtk0w43dbqru0qq30gu4xwsm3g23dg3vwha4xn3pdqmvdqsm0qe6xgqbuqu6ds2mwqmw0q2v2he是一个动态UTI,它编码"?0=6:1=202z--dacaf3645999623f2855ecbb696a9300d87a2befb89"。使用动态类型的事实表明您的 UTI 注册出现问题。 -
谢谢@Alexander 你是说 Info.list 文件无效吗?哪一部分错了?
-
我是这么认为的,但我不知道具体是什么。我记得以前在正确注册我的 UTI 时遇到了困难,但我不记得细节了。
-
能否以文本形式分享您的
info.plist(文档类型、导入/导出的UTI 等)的相关部分?此外,清理所有多余的空白区域。我知道添加一些是为了视觉清晰,但Document实际上是双倍行距。 -
如何以文本形式共享 info.list 文件?我不知道怎么做,因为我是 iOS 的初学者。
标签: ios swift file cryptography document