在应用程序扩展中,是否有一种方法可以获取文件并将其复制到Documents//文件夹?
我可以用下面的代码得到文件。但如何复制它?我总是犯错误
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
for provider in item.attachments! as! [NSItemProvider] {
provider.loadItem(forTypeIdentifier: provider.registeredTypeIdentifiers.first! as! String, options: nil, completionHandler: { (fileURL, error) in
if let fileURL = fileURL as? URL {
self.url = fileURL
// self.extensionOpenUrl(App.associatedDomain + fileURL.absoluteString)
}
})
}
}
点击复制:
let fileManager = FileManager.default
let pathForDocumentsDirectory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: App.group)!.path
let fileURL = self.url!
let name = fileURL.lastPathComponent
let copiedPath = pathForDocumentsDirectory
do {
try fileManager.copyItem(atPath: fileURL.absoluteString, toPath: copiedPath)
if fileManager.fileExists(atPath: copiedPath) {
print("fileExists!!!")
}
} catch let error as NSError {
print("error in copyItemAtPath")
print(error.localizedDescription)
}
文件url:
file:///var/mobile/Media/PhotoData/OutgoingTemp/3CEC8D4A-9B1B-468B-A919-7C70C9C522B3/IMG_5484.jpg
要复制的路径:
/private/var/mobile/Containers/Shared/AppGroup/D7D2317B-9C57-424D-9D2F-209C62BBFAE5/IMG_5484.jpg
错误类型:
The file “IMG_5484.jpg” couldn’t be opened because you don’t have permission to view it.
2条答案
按热度按时间jc3wubiy1#
你不能这么做扩展只能通过保留/修改应用程序组空间中的文件来与主应用程序通信(这也意味着您必须首先在开发人员门户中创建应用程序组并添加适当的权利)。
mqkwyuun2#
扩展不能访问容器应用的文档目录(或主应用的任何目录)。正如有人已经指出的那样,你可以设置一个应用组并将文件复制到应用组共享目录中,然后主应用将可以访问共享应用组文件夹中的文件。
url
是您希望从扩展共享到主应用程序的文件URL。完成此操作后,您可能还希望激活主应用程序,以便主应用程序可以根据应用程序的逻辑处理/使用共享文件。