我有一个iOS应用程序,我想保存一个文件,当Safari上的用户下载文件并选择“导入MYAPP”时,该文件将被提供给它
我已经知道我的应用程序支持该文件的部分,但是当我选择使用应用程序打开文件时,它没有被添加。
实际上没有太多关于此的文档,或者至少没有最近的文档。这是我保存文件失败的尝试:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?, openURL Url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
let encrypteddata = NSData(contentsOf: Url as URL)
if(encrypteddata != nil){
let fileManager = FileManager.default
let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("test.ipa")
print(paths)
fileManager.createFile(atPath: paths as String, contents: encrypteddata as Data?, attributes: nil)
}
return true
}
更新:
似乎这些文件存储在应用程序沙箱容器的Documents / Inbox目录中。如何列出所有文件并阅读其内容?
一旦你有URL
optional func application(_ application: UIApplication, open url: URL,
sourceApplication: String?,
annotation: Any) -> Bool
方法,您可以从此URL访问文件,或者您可以从FileManager中找到所有文档,如下所示
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
print(directoryContents)
} catch let error as NSError {
print(error.localizedDescription)
}
这将列出文档目录中的所有文件。