我有一个HTML文档,其中包含运行JavaScript代码的链接:
<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所有这一切正常
这是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,所以我假设它与布局有关,Here is another similar question
// 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
对于大量的代码感到抱歉,我已经停留在这个View / Download PDF上5天了,任何帮助都会很棒