Reputation: 401
I want the user to select a file from files app and I've to read the contents of that file, modify it and write it in the same location.
I was trying to open the file using the following code:
UIDocumentPickerViewController *documentProvider;
documentProvider = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:[NSArray arrayWithObjects:@"public.comma-separated-values-text", nil] inMode: UIDocumentPickerModeOpen];
documentProvider.delegate = self;
documentProvider.modalPresentationStyle = UIModalPresentationOverFullScreen;
Delegate function:
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
{
NSLog(@"%@", url.absoluteString);
}
But when I try to view or edit the file, it's showing error that I don't have view/write permission.
The URL that I received is file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/filename.csv
Is there any way to get permission for the files in iCloud? I found few third party apps which can modify the contents of the file.
Upvotes: 2
Views: 2476
Reputation: 17677
You need to access the file via a security scoped bookmark. Like such:
- (void) documentPicker: (UIDocumentPickerViewController *) controller
didPickDocumentAtURL: (NSURL *) url
{
if (controller.documentPickerMode == UIDocumentPickerModeOpen)
{
BOOL isAccess = [url startAccessingSecurityScopedResource];
if(!isAccess)
{
return;
}
NSError * fileOpenError = nil;
NSString * fileContents = [NSString stringWithContentsOfURL: url
encoding: NSUTF8StringEncoding
error: &fileOpenError];
// FileContents should now be set properly.
[url stopAccessingSecurityScopedResource];
}
}
Upvotes: 5