presentOpenInMenuFromRect无法正常工作DocumentHandler.h - QuickLook

时间:2014-11-19 22:50:14

标签: ios cordova quicklook presentviewcontroller

我正在使用documenthandler cordova插件,如果我单击按钮,我会从url中获取文档处理程序中的pdf,工作正常,以便我可以将pdf保存到iBooks中。

现在,不是在查看器中打开文档并单击共享按钮,然后再次单击以保存到iBooks,我需要能够在不打开文档的情况下触发共享按钮。我知道这可以使用presentOpenInMenuFromRect代替presentViewController来完成,但由于某些原因它无效,代码如下:

#import "DocumentHandler.h"

@implementation DocumentHandler

- (void)HandleDocumentWithURL:(CDVInvokedUrlCommand*)command;
{
    CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];

    __weak DocumentHandler* weakSelf = self;

    dispatch_queue_t asyncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(asyncQueue, ^{

        NSDictionary* dict = [command.arguments objectAtIndex:0];

        NSString* urlStr = dict[@"url"];
        NSURL* url = [NSURL URLWithString:urlStr];
        NSData* dat = [NSData dataWithContentsOfURL:url];
        NSString* fileName = [url lastPathComponent];
        NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent: fileName];
        NSURL* tmpFileUrl = [[NSURL alloc] initFileURLWithPath:path];
        [dat writeToURL:tmpFileUrl atomically:YES];
        weakSelf.fileUrl = tmpFileUrl;

        dispatch_async(dispatch_get_main_queue(), ^{
            QLPreviewController* cntr = [[QLPreviewController alloc] init];
            cntr.delegate = weakSelf;
            cntr.dataSource = weakSelf;

            UIViewController* root = [[[UIApplication sharedApplication] keyWindow] rootViewController];

            [root presentViewController:cntr animated:YES completion:nil];//this works fine and open the document with share button

            CGRect rect = CGRectMake(0, 0, 1024, 768);
            [root presentOpenInMenuFromRect:rect inView:self.view animated:YES]; // this doesn't work where
            //I want to see only sharing options
            //here are errors,one of them is /Property'view' not found on object of type ''DocumentHandler
        });


        [weakSelf.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
    });
}



#pragma mark - QLPreviewController data source

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

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

#pragma mark - QLPreviewItem protocol

- (NSURL*)previewItemURL
{
    return self.fileUrl;
}

@end

我需要帮助:(

编辑:看图像我想要实现的目标:

enter image description here

1 个答案:

答案 0 :(得分:2)

presentOpenInMenuFromRect是一种UIDocumentInteractionController方法。我不认为你在这段代码中使用了一个,除非你的根视图控制器是UIDocumentInteractionController,这将非常奇怪。

不是实例化和呈现QLPreviewController,而是实例化UIDocumentInteractionController并从对应于文档图标的矩形中显示弹出框。

为此,请查看UIDocumentInteractionController documentation。您将看到有interactionControllerWithURL:方法可用于实例化指向您文件的UIDocumentInteractionController。然后,您可以致电 presentOpenInMenuFromRect:inView:animated:以显示所需的弹出窗口。