I am trying to load files in device by using UIDocumentInteractionController in a phonegap app. I have done by following.

#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>

@interface FileViewer : UIViewController <UIDocumentInteractionControllerDelegate>

@end

@implementation FileViewer

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (UIViewController *) documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    return self;
}

- (void)viewDidLoad
{
    [self viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

In Launcher.m i have done by following to show files:

    CDVViewController* mainController = (CDVViewController*)[ super viewController ];
        UIDocumentInteractionController *documentInteractionController =    [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileLocation]];
        FileViewer *vController = [[FileViewer alloc]init];
        [mainController addChildViewController:vController];
        documentInteractionController.delegate = vController;
       [documentInteractionController presentPreviewAnimated:YES];

This result by giving error message: "Presenting view controllers on detached view controllers is discouraged. Unbalanced calls to begin/end appearance transitions for QLRemotePreviewContentController"

Thanks in advance!!

1

There are 1 answers

0
RamGrg On

I have solved my issue by following way. It might not be the best way, so please suggest the best way also.

@interface Launcher : CDVPlugin<UIDocumentInteractionControllerDelegate>
@property (strong, nonatomic)UIViewController *navigatorController;
@property (strong, nonatomic)Launcher *launcher;
@property (strong, nonatomic)NSString *callbackId;
-(void)openFile:(CDVInvokedUrlCommand*)command;

@end

#import "Launcher.h"
#import "FileViewer.h"
#import<QuickLook/QuickLook.h>
#import <MobileCoreServices/MobileCoreServices.h>

@implementation Launcher

-(void)openFile:(CDVInvokedUrlCommand*)command{
    CDVPluginResult *pluginResult = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSDictionary *params = [command.arguments objectAtIndex:0];
    NSString *file = [params objectForKey:@"message"];
    NSString *fileLocation = [documentsDirectory stringByAppendingFormat:@"/%@",file];

    if (![fileLocation isEqual:[NSNull null]]) {
        @try {
            self.launcher = self;
            CDVViewController *viewController = [CDVViewController new];
            viewController.wwwFolderName = @"www";
            viewController.startPage = @"main.html";  // page contains listview showing name of files
            self.navigatorController = viewController;
            UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileLocation]];
            [[UIApplication sharedApplication].keyWindow setRootViewController:self.navigatorController];
            documentInteractionController.delegate = self;
            [documentInteractionController presentPreviewAnimated:YES];    
        }
        @catch (NSException *exception) {
            NSLog(@"dd : %@",[exception reason]);
        }

    }
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:  (UIDocumentInteractionController *)controller
{
    return self.navigatorController;
}

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [[UIApplication sharedApplication].keyWindow setRootViewController:self.navigatorController];
    self.launcher = nil;
}