繁体   English   中英

iOS:应用扩展程序如何将文件复制到应用文档/文件夹?

[英]iOS: How can app extension copy file to app Documents/ folder?

在应用扩展中,是否有一种获取文件并将其复制到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)
    }

文件网址:

 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.

你不能那样做。 扩展只能通过在应用程序组空间中保留/修改文件来与主应用程序通信(这也意味着您必须首先在开发人员门户中创建应用程序组并添加适当的权利)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM