UIDocumentInteractionController

UIDocumentInteractionController

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

现在,与其在查看器中打开文档并单击“共享”按钮,然后再次单击以保存到iBook中,我需要能够在不打开文档的情况下触发共享按钮。我知道可以使用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


我需要帮助:(

编辑:看到图像,我正在试图实现:

最佳答案

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

而不是实例化并呈现QLPreviewController,而是实例化UIDocumentInteractionController并呈现与文档图标相对应的rect中的弹出窗口。

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

关于ios - presentOpenInMenuFromRect无法正常工作DocumentHandler.h-QuickLook,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27028539/

10-10 02:17