3

Hi in my app I have to get file from iCloud.. I enable iCloud all working fine in Simulator.. But when I test in iPhone, I can see the iCloud files when I choose the default delegate calls but I can't get data from the url..

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
{
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:url];
} 

Always I getting pdfData as nil. I log the error

Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x17029810 {NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/download (2).jpeg, NSUnderlyingError=0x15ea9680 "The operation couldn’t be completed. Operation not permitted"}

I cant figure out.. Can you please help me to fix this issue..

TamilKing
  • 1,643
  • 1
  • 12
  • 23

1 Answers1

13

Finally I found answer myself.. by refer this link Here

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
{
    [url startAccessingSecurityScopedResource];
    __block NSData *pdfData = nil;

    NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
    __block NSError *error;
    [coordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
        pdfData = [NSData dataWithContentsOfURL:newURL];
    }];
    [url stopAccessingSecurityScopedResource];
}
Community
  • 1
  • 1
TamilKing
  • 1,643
  • 1
  • 12
  • 23