将文件导入应用后保存文件

时间:2017-08-03 06:24:08

标签: ios swift import

我有一个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目录中。如何列出所有文件并阅读其内容?

1 个答案:

答案 0 :(得分:0)

获取网址后
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)
}

这将列出文档目录中的所有文件。