2

我有一个带有运行 JavaScript 代码的链接的 HTML 文档:

    <a href="http://www.tricedesigns.com/temp/drm.pdf" onclick="app.openExternalDoc();">Open pdf</button>
    <script type="text/javascript" src="../assets/js/index.js"></script>
    <script type="text/javascript" src="../assets/js/ExternalFileUtil.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
    <script src="../cordova.js"></script>

index.js一切正常

ExternalFileUtil.js也是如此。

这是 CDVExternalFileUtil.h 文件:

#import <Cordova/CDV.h>

@interface CDVExternalFileUtil : CDVPlugin <UIDocumentInteractionControllerDelegate> {
    NSString *localFile;
}

- (void) openWith:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) cleanupTempFile: (UIDocumentInteractionController *) controller;

@end

这是 CDVExternalFileUtil.m 文件:

#import "CDVExternalFileUtil.h"

@implementation CDVExternalFileUtil


- (void) openWith:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{   
    CDVPluginResult* pluginResult;
    NSString* callbackID = [arguments pop];
    [callbackID retain];

    NSString *path = [arguments objectAtIndex:0]; 
    [path retain];

    NSString *uti = [arguments objectAtIndex:1]; 
    [uti retain]; 

    NSLog(@"path %@, uti:%@", path, uti);

    NSArray *parts = [path componentsSeparatedByString:@"/"];
    NSString *previewDocumentFileName = [parts lastObject];
    NSLog(@"The file name is %@", previewDocumentFileName);

    NSData *fileRemote = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:path]];

    // Write file to the Documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {NSLog(@"Documents directory not found!");}
    localFile = [documentsDirectory stringByAppendingPathComponent:previewDocumentFileName];
    [localFile retain];
    [fileRemote writeToFile:localFile atomically:YES];
    NSLog(@"Resource file '%@' has been written to the Documents directory from online", previewDocumentFileName);

问题似乎出在这个区域(因为它适用于 iPhone,但不适用于 iPad,所以我认为它与布局有关,这是另一个类似的问题

    // Get file again from Documents directory
    NSURL *fileURL = [NSURL fileURLWithPath:localFile];

    UIDocumentInteractionController *controller = [UIDocumentInteractionController  interactionControllerWithURL:fileURL];
    controller.delegate = self;
    controller.UTI = uti;
    [controller retain];

    CDVViewController* cont = (CDVViewController*)[ super viewController ];
    CGRect rect = CGRectMake(0, 0, cont.view.bounds.size.width, cont.view.bounds.size.height);
    [controller presentOptionsMenuFromRect:rect inView:cont.view animated:YES];

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
    [self writeJavascript: [pluginResult toSuccessCallbackString:callbackID]];

    [callbackID release];
    [path release];
    [uti release];
}

- (void) documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
    NSLog(@"documentInteractionControllerDidDismissOpenInMenu");

    [self cleanupTempFile:controller];
}

- (void) documentInteractionController: (UIDocumentInteractionController *) controller didEndSendingToApplication: (NSString *) application {
    NSLog(@"didEndSendingToApplication: %@", application);

    [self cleanupTempFile:controller];
}

- (void) cleanupTempFile: (UIDocumentInteractionController *) controller
{

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    BOOL fileExists = [fileManager fileExistsAtPath:localFile];   
    NSLog(@"Path to file: %@", localFile);
    NSLog(@"File exists: %d", fileExists);
    NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:localFile]);



    if (fileExists) 
    {
        BOOL success = [fileManager removeItemAtPath:localFile error:&error];
        if (!success) NSLog(@"Error: %@", [error localizedDescription]);
    }
    [localFile release];
    [controller release];
}

@end

很抱歉有大量代码,我已经在这个查看/下载 PDF 上停留了 5 天,任何帮助都会很棒

4

0 回答 0