Bug Hunter Zoro
Bug Hunter Zoro

Reputation: 1915

Read file using Office 365 SDK for iOS

I am trying to read a file using the Office 365 SDK for iOS using their sample demo i am able to fetch the files list.

Now I am stuck at the place where I want to read the content of the files, given below is the code that am using

    MSSharePointFile* fileToRead = [self.Files objectAtIndex:indexPath.row];

    [BaseController getClient:^(MSSharePointClient * client) {

        MSSharePointItemFetcher *fetcher = [[client getfiles] getById:fileToRead.id];

        MSSharePointFileFetcher *fileFetcher = [fetcher asFile];

        [[fileFetcher read:^(MSSharePointFile *file, MSODataException *error) {

            NSLog(@"%@",file.contentUrl);

            // download item from content URL
           //Am stuck at this point

        }] resume];

I am able to fetch the contentURL for the selected file but i am stuck at what needs to be done next to extract file text. I traversed through the documentation but failed.

Please help me out on this.

Thanks

Upvotes: 2

Views: 142

Answers (1)

thecanteen
thecanteen

Reputation: 784

This is pretty straightforward -- here's some code that will get you exactly what you want. Note that you have to replace "myId" with the ID of the file you want to get the data of. This code snippet below will encode the data in UTF8 and print it as a string.

[BaseController getClient:^(MSSharePointClient *client) {
    [[[[[client getfiles] getById:myId] asFile] getContentWithCallback:^(NSData *content, MSODataException *error) {
        if (error == nil) {
            NSString *fileContent = [[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding];
            NSLog(@"Data in file is %@", fileContent);
        }
    }] resume];
}];

Upvotes: 2

Related Questions