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