0

Is there any way I can show Open With or Open In window for ms office files I have seen UIDocumentInteractionController and QuickLook Framework but didn't work me. Does these two really support office files?

Arun
  • 31
  • 1
  • 3

2 Answers2

0

Quicklook must work

{
    QLPreviewController *previewer = [[QLPreviewController alloc] init];
    previewer.dataSource = datasource;
    previewer.currentPreviewItemIndex = 0; 
    NSURL *urlFilePath = [[NSBundle mainBundle] URLForResource:@"name" withExtension:@"pdf"];
    [self presentViewController:previewer animated:YES completion:nil];
}

-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1;
}

  - (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    return urlFilePath;
}

where urlFilePath is file path url.

Rajesh
  • 10,318
  • 16
  • 44
  • 64
  • It is opening everything except for word, excel, pptx etc. It is just keep on loading for these files. Do I need to add anything to make ms office files to get opened? – Arun Jun 09 '16 at 07:05
0

First of all Add "QuickLook.framework" to your project. Its part of the iOS SDK frameworks. Get the names of all files present in document directory in listOfFilesPresentInDocumentDirectory NSArray which is defined as the class variable.

NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
listOfFilesPresentInDocumentDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectory error:NULL];

Create an object of QLPreviewController, set self as its datasource, set its currentPreviewItemIndex and push it to the UINavigationController

QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.currentPreviewItemIndex = indexPath.row;
[[self navigationController] pushViewController:previewController animated:YES];

Implement QLPreviewControllerDataSource protocol in you class’s definition and add following two QLPreviewControllerDataSource functions in the implementation block of your class

pragma mark - QLPreviewControllerDataSource Methods

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
return [listOfFilesPresentInDocumentDirectory count];
}

- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
 NSURL *fileURL = nil;
 NSString *fileName = [listOfFilesPresentInDocumentDirectory objectAtIndex:index];
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString *previewFileFullPath = [documentDirectory stringByAppendingPathComponent:fileName];
fileURL = [NSURL fileURLWithPath:previewFileFullPath];
return fileURL;

}

Community
  • 1
  • 1
neha
  • 167
  • 2
  • 11