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)")
}
}
)
}
}
}