我正在尝试通过在phonegap应用程序中使用UIDocumentInteractionController来加载设备中的文件。我已经做到了以下几点。
#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
在Launcher.m中,我通过以下方式显示文件:
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];这是通过给出错误消息:“不鼓励在分离的视图控制器上呈现视图控制器。不平衡调用QLRemotePreviewContentController的开始/结束外观转换”造成的。
提前感谢!!
发布于 2014-11-26 20:16:50
我已经通过下面的方式解决了我的问题。这可能不是最好的方法,所以请也建议最好的方法。
@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;
}https://stackoverflow.com/questions/27010351
复制相似问题