【发布时间】:2016-04-13 16:14:24
【问题描述】:
我正在创建一个 iOS 应用程序,用户可以通过它在他们的设备上打印文件。在我的应用程序中,我可以通过 iCloud Drive、Dropbox 等其他应用程序提供的DocumentPicker 访问设备上的文件。
现在,我想添加一个功能,用户可以通过其他应用程序与我的应用程序共享文件。我为此创建了一个Action Extension。
例如,如果我在照片应用程序中选择一个图像并选择Share,我会在共享表中获得我的扩展名,当我选择它时,我还会获得文件的 URL。接下来,我正在创建此文件的 zip 文件以将其发送到我的服务器。但问题是,zip 文件总是空的。我使用的代码如下:
In Action Extension 的 viewDidLoad()
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
itemProvider.loadItemForTypeIdentifier(kUTTypeImage as String, options: nil,
completionHandler: { (image, error) in
NSOperationQueue.mainQueue().addOperationWithBlock {
print("Image: \(image.debugDescription)")
//Image: Optional(file:///Users/guestUser/Library/Developer/CoreSimulator/Devices/00B81632-041E-47B1-BACD-2F15F114AA2D/data/Media/DCIM/100APPLE/IMG_0004.JPG)
print("Image class: \(image.dynamicType)")
//Image class: Optional<NSSecureCoding>
self.filePaths.append(image.debugDescription)
let zipPath = self.createZip(filePaths)
print("Zip: \(zipPath)")
}
})
}
而我的createZip函数如下:
func createZipWithFiles(filePaths: [AnyObject]) -> String {
let zipPath = createZipPath() //Creates an unique zip file name
let success = SSZipArchive.createZipFileAtPath(zipPath, withFilesAtPaths: filePaths)
if success {
return zipPath
}
else {
return "zip prepation failed"
}
}
有没有一种方法可以创建共享文件的 zip?
【问题讨论】:
-
SSZipArchive是否返回错误?你确定你有对zipPath的写访问权和对filePaths中所有路径的读访问权吗? -
不,它不会给出任何错误。它在
zipPath创建一个zip 文件,因此我在zipPath具有写访问权限。而且,filePaths中的文件是来自默认照片应用程序的图像文件。在日志中它说文件在提到的位置不存在,但是如果我通过终端导航(对于模拟器),我可以看到该文件。 -
使用
image.debugDescription不是获取图像文件路径的方法。首先,完成处理程序中image的实际类型是什么?image.debugDescription的输出是什么? -
@rmaddy: 图像类型为
NSSecureCoding,image.debugDescription的输出类似于fie:///Users/xxx/Library/Developer/CoreSimulator/Devices/---xx-xx-xx/data/Media/DCIM/100APPLE/IMG_0004.jpg -
您已发布 2 功能。第一次使用 self.createZip(filePaths),第二次使用 func createZipWithFiles,但我没有看到任何 createZip 方法..
标签: ios swift ios-extensions ios-sharesheet