iOS 13 How to use UIDocumentPickerViewController with App Group? - ios

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.

Related

iOS Custom Notification Sound (UNNotificationSound) doesn't play when iPhone is locked

My app uses remote notifications with a NotificationService Extension in which I edit the notification before displaying it.
I would like to let the user upload a custom sound file which should be played instead of the default sound. For this I use an shared AppGroup, which the app and the extension have access to.
The uploaded sound files are stored in the "Library/Sounds" directory as follows (my code for testing, without much error handling):
.....
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xxx.xxx")
let soundsURL = containerURL!.appendingPathComponent("Library/Sounds/", isDirectory: true)
if !FileManager.default.fileExists(atPath: soundsURL.path) {
try! FileManager.default.createDirectory(atPath: soundsURL.path, withIntermediateDirectories: true)
}
if FileManager.default.fileExists(atPath: soundsURL.path) {
do {
try FileManager.default.copyItem(at: sourceURL, to: soundsURL.appendingPathComponent(sourceURL.lastPathComponent))
} catch {
// Exception
}
}
In the Notification Extension I change the sound of the notification to the name of the uploaded file:
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "test.wav"))
This is working fine as long as the iPhone is not locked. But if the iPhone is locked, there is no vibration and no sound is played (also no default sound). But I don't know why - according to apples documentation UNNotificationSound looks in "Library/Sounds" of the app shared group container directories. If I store the file directly in the main bundle, it works.
Does anyone have any idea what could be causing this?
Ok, now i figured out what the problem is.
My files were created with "NSFileProtectionComplete" data protection by default.
"NSFileProtectionComplete — The file is only accessible while the device is unlocked."
After changing the data protection of my sound files to "NSFileProtectionNone", it finally works!

Choose destination when saving file

Is it possible to make user able to choose a destination for the file that he wants to download, something like DocumentPicker which you can use when choosing a file to upload?
I want something like this:
Yes, for iOS 13 and later you can ask the user to select a directory via UIDocumentPickerViewController. You'll get back a security scoped url(s) for the directories selected by the user.
Details here: https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories
I've pasted the sample code from that page below, but you'll want to read the documentation carefully because security scoped URLs require careful handling :)
If you need iOS 12 or earlier the user can only select files so I'm unclear on a clean way to do this (but we're on iOS 14 and iOS 15 is about to come out so hopefully you don't have to support back past iOS 13).
Here's the sample code from the link above showing how this is done:
// Create a document picker for directories.
let documentPicker =
UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
documentPicker.delegate = self
// Set the initial directory.
documentPicker.directoryURL = startingDirectory
// Present the document picker.
present(documentPicker, animated: true, completion: nil)

When picking a folder from UIDocumentPickerViewController, third-party file providers are grayed out?

I have a UIDocumentPickerViewController with a filetype of "public.folder" (I've also tried kUTTypeFolder), where a user can pick a default directory for files to be saved. It pulls up the correct UI for selecting folders, however, all third-party providers (Google Drive, Dropbox, etc.) are all grayed out and can't be selected. I can select iCloud Drive and On My iPhone just fine.
Here's my function for showing the controller:
#IBAction func pickDefaultDirectory(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.folder"], in: .open)
documentPicker.delegate = self
self.present(documentPicker, animated: true, completion: nil)
}
This is what every "pick folders via UIDocumentPickerViewController" article I've found says to do, but I've had no luck. Does anyone know why this is happening? Am I just forgetting something in there?
According to Dropbox developers, this isn't supported at the moment.
The Dropbox document picker doesn't support opening folders, but I'll pass this along as a feature request. I can't promise if or when that might be implemented though.
Dropboxforum

Files with imported UTIs are greyed out in UIDocumentPickerViewController

In my iOS app I am opening a UIDocumentPickerViewController to import .sty files (MIDI styles). For this I have declared a custom UTI in Imported UTIs and in Document Types.
The problem is that on my customer’s iPad Air 2, these files appear greyed out in the dialog so he cannot import them. On my iPad Air 2 they are not greyed out, and I can import them successfully. We can both see this when using iCloud Drive and Dropbox as file providers.
What could be different on our devices? My customer installs the app as an internal tester via TestFlight.
Also, are file extensions in the Import UTIs case-sensitive? I would not think so.
Then I wonder if I should add a document type icon or not. According to the reference the icon is not required.
iCloud Documents is enabled:
Here is the code that presents the UIDocumentPickerViewController:
let utis = [String](PlaylistItem.utiToType.keys)
let viewController = UIDocumentPickerViewController(documentTypes: utis, in: .import)
viewController.delegate = self
viewController.modalPresentationStyle = .formSheet
if #available(iOS 11, *) {
viewController.allowsMultipleSelection = true
}
self.present(viewController, animated: AppDelegate.isAnimationsEnabled, completion: nil)
with this definition of UTIs in the PlaylistItem class:
enum FileType: Int {
case other
case mid
case mp3
case m4a
case aiff // AIFF audio recording
case wave
case turboMidi // purchased MIDI file
case style
}
static let kUTTypeStyleYamaha = "com.turboreini.style" // matches document types in Info.plist
static let utiToType: [String: FileType] = [
kUTTypeMPEG4Audio as String: .m4a,
kUTTypeMP3 as String: .mp3,
kUTTypeMIDIAudio as String: .mid,
PlaylistItem.kUTTypeStyleYamaha: .style
]
This is the Document Type as captured from Xcode 9:
… and the Imported UTIs section:
Note that I picked an arbitrary identifier for style files because I couldn’t find an official one on the internet.
This issue drives me crazy. I cannot find anything wrong.
I wonder if it depends on other installed apps.
In addition to importing .sty files, the app also creates its own custom files with their own extension. For this I have defined an Exported UTI. It works fine, no files are greyed out. But the solution to importing .sty files should not be to move the entry from Imported to Exported UTIs. My app is only a “viewer” to .sty files.
Any ideas on this case would be much appreciated.
It turned out that on my customer’s iPad another app was installed that presumably also had its own UTI for the .sty extension. Once I installed it on my iPad I could reproduce the problem.
Unfortunately I was not able yet to peek into that app’s Info.plist to confirm.
However, thanks to Cocoanetics I found an API that allowed me to query the system:
let pathExt = "sty"
if let utiArray = UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension, pathExt as NSString, nil)?.takeRetainedValue() as? [String] {
print("Have UTIs for .\(pathExt):")
for uti in utiArray {
if let dict = UTTypeCopyDeclaration(uti as NSString)?.takeUnretainedValue() as? [String: Any] {
print("\(uti) = \(dict)")
}
}
}
This code would show me the declared UTIs from all the apps installed on my iPad, based on the file extension. And indeed the other app showed up. But I still could not see if it was declared as an exported or imported UTI.
The workaround is to uninstall the conflicting app.
To me this is a flaw in the UTI system. I will try and contact the makers of .sty files (MIDI styles, not LaTeX files), to ask them for a proper UTI which all developers should conform to.
It seems a global registry of UTIs is needed. Apple has already created a collection but only the most popular types are covered.

UIDocumentPickerViewController -> "No Documents" because iCloud not available

I use this method to show an UIDocumentPicker:
func showDocumentPicker(){
let docPicker = UIDocumentPickerViewController(documentTypes: ["public.composite-content "], inMode: UIDocumentPickerMode.Import)
docPicker.delegate = self
docPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
self.presentViewController(docPicker, animated: true, completion: nil)
}
The UIDocumentPicker gets displayed nicely, but it always shows
No Document, Documents in iCloud Drive are not available because the iCloud Documents & Data setting is disabled
But when I check the iCloud Status, iCloud Drive is enabled! (My App even shows up in the settings there, also enabled!)
This happens in Simulator and on the device (via a Prerelease distributed by Apple TestFlight)
This error can be caused due to invalid UTI-constants:
Make sure to double-check the UTIs you pass to the documentTypes parameter. In this case, note the blank space in the public.composite-content string

Resources