简体   繁体   中英

iOS 13 How to use UIDocumentPickerViewController with App Group?

I have implemented an AppGroup in my app in preparation for sharing data with another app. I have successfully moved files to that App Group from the default app documents directory.

FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xxx.mydata")! as NSURL

Now I would like to select from the files in that container, using UIDocumentPickerViewController. In iOS 13,I should be able to set which directory the document picker starts in. My documentPicker looks like this:

@IBAction func fileAction(_ sender: UIButton)
{
    // open a document picker, select a file
    let importFileMenu = UIDocumentPickerViewController(documentTypes: ["public.data"], 
            in: UIDocumentPickerMode.import)
    importFileMenu.delegate = self
    if #available(iOS 13.0, *) {
        print("File iOS 13+")
        importFileMenu.directoryURL = FileManager.default.containerURL(
            forSecurityApplicationGroupIdentifier: "group.com.xxx.mydata")!
    } else {
        // Fallback on earlier versions
        print("File iOS <=12")
    }
    importFileMenu.modalPresentationStyle = .formSheet

    self.present(importFileMenu, animated: true, completion: nil)
}

When I run the app, it acts as it did before iOS13, opening in the default app documents directory, and the App Group is not shown as a possibility for selection. The print statement shows "File iOS 13+".

Am I missing permissions to read from that container, or is there something else that I've missed? Thanks!

No, sorry, it can't be done. Apple says that selecting from an AppGroup is not what UIDocumentPickerViewController is supposed to do. I spent one of my "Apple Developer Tech Support" uses on this, and that was their answer. I gave up on that for now, and went a different direction. You should be able to build your own list of files in the AppGroup and select them, just not by using UIDocumentPickerViewController.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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