My app needs to share data files with multiple devices owned by a single user. I also want to implement this mechanism without setting up a server. Therefore, I want to make it read/write to local data on the user's own cloud drive (e.g. iCloud, Google Drive, One Drive, Dropbox) and read/use them as needed.
I have tried “.fileImporter” to get the URL, but the button is grayed out and cannot be opened.
Sorry for my poor English.
struct FilePathSettingView: View {
@State private var isPickerPresented: Bool = false
var fileControll = FileControll()
var body: some View {
VStack {
Text("Storage Setting")
Button(action:{
isPickerPresented = true
})
{
Text("Select place")
}
.fileImporter(
isPresented: $isPickerPresented,
allowedContentTypes: [.directory, .folder],
allowsMultipleSelection: false,
onCompletion: { result in
switch result {
case .success(let urls):
guard let url = urls.first else { return }
let accessGranted = url.startAccessingSecurityScopedResource()
defer {
if accessGranted {
url.stopAccessingSecurityScopedResource()
}
}
guard accessGranted else {
print("Failed to access security-scoped resource.")
return
}
fileControll.createDirectoryStructure(in: url)
case .failure(let error):
print("Failed to select directory: \(error.localizedDescription)")
}
}
)
}
}
}
"I have confirmed that the iCloud Drive folder works as is."
That's great.
"As for the file provider, I have not been able to confirm due to my lack of knowledge."
You can follow the Readme to set up the file provider on macOS or iOS. The Configure the sample code project section covers that in great details.
"Also, Google Drive, One Drive, and Dropbox continue to not allow me to select a folder."
That needs the 3rd-party file provider vendors to adopt NSFileProviderReplicatedExtension
, like what the mentioned sample does. You might consider providing your feedback to the vendors directly.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.