1

我一直在阅读,但我还没有找到解决方案。我正在使用 FileTransfer 成功下载 pdf 文件。它位于名为 Documents 的文件夹中,位置如下所示:/Users/myusername/Library/Application Support/iPhone Simulator/5.1/Applications/C62E5802-56A8-48BF-B57C-695801F3C8D6/HelloWorld.app/Documents/11。 pdf

我正在尝试使用 ChildBrowser 打开它,但到目前为止我没有成功。

打开的代码是这样的:

cordova.exec("ChildBrowserCommand.showWebPage", path );

当路径是外部的(例如http://www.google.com/)时,它运行良好(将域列入白名单)。

我尝试了所有这些路径都没有成功:

file:///Users/myusername/Library/Application Support/iPhone Simulator/5.1/Applications/C62E5802-56A8-48BF-B57C-695801F3C8D6/HelloWorld.app/Documents/11.pdf

file:////Users/myusername/Library/Application Support/iPhone Simulator/5.1/Applications/C62E5802-56A8-48BF-B57C-695801F3C8D6/HelloWorld.app/www/Documents/11.pdf

文件:///Documents/11.pdf

我需要做什么才能达到它?查看上面的应用程序位置,我也找不到 www 文件夹(我假设它已被打包)

这一切都在模拟器、cordova 2.1.0 和最新的 ChildBrowser 中。

4

3 回答 3

3

我在这里找到了另一个回答我的问题。关联

要归档的 uri 应该是这样的:

file:///Users/{username}/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/F815A351-44EC-46E8-AD00-A7D775FF9ECC/Documents/11.pdf

如果文件已下载到应用程序的 Documents 文件夹中,而不是放在 www 文件夹中。获取这样的网址:

fp = entry.fullPath;
urld = "file://" + fp;
//open the file in ChildBrowser
cordova.exec("ChildBrowserCommand.showWebPage", encodeURI(urld) );
于 2012-10-28T21:45:34.327 回答
3

我已将https://github.com/vfr/Reader PDF 阅读器添加到 ChildBrowser。效果超级棒!

当前组件版本:Cordova 2.2.0 的子浏览器

编辑 ChildBrowserCommand.h:

#import <Cordova/CDVPlugin.h>
#import "ChildBrowserViewController.h"
#import "ReaderDemoController.h"
#import "ReaderViewController.h"
@interface ChildBrowserCommand : CDVPlugin <ChildBrowserDelegate, ReaderViewControllerDelegate>{}

@property (nonatomic, strong) ChildBrowserViewController* childBrowser;
@property (nonatomic, strong) ReaderDemoController* reader;

- (void)showWebPage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void)onChildLocationChange:(NSString*)newLoc;

@end

在 .M 文件中 1)@synthesize reader;

2)

- (void)showWebPage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options  
{
    NSString* url = (NSString*)[arguments objectAtIndex:0];
    if([url hasSuffix:@".pdf"])
    {
        NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
        ReaderDocument *document = [ReaderDocument withDocumentFilePath:url password:phrase];
        ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];'

        readerViewController.delegate = self; // Set the ReaderViewController delegate to self
        readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;

        [self.viewController presentModalViewController:readerViewController animated:YES];
    }
    else
    {
        if (self.childBrowser == nil) {
#if __has_feature(objc_arc)
            self.childBrowser = [[ChildBrowserViewController alloc] initWithScale:NO];
            NSLog(@"HAS ARC");
#else
            self.childBrowser = [[[ChildBrowserViewController alloc] initWithScale:NO] autorelease];
            NSLog(@"NO ARC");
#endif
            self.childBrowser.delegate = self;
            self.childBrowser.orientationDelegate = self.viewController;
        }
        [self.viewController presentModalViewController:childBrowser animated:YES];
        [self.childBrowser loadURL:url];
    }
}

3)

-(void)dismissReaderViewController:(ReaderViewController *)viewController{
    NSLog(@"The delegate to dismiss pdf reader was called");
   [self.viewController dismissModalViewControllerAnimated:YES];}

4) 将阅读器中的所有内容复制到我们的项目中,添加所需的所有框架

享受。不要忘记打开阅读器的 ARC

于 2012-11-23T12:36:18.850 回答
2
childbrowser = ChildBrowser.install();
console.log(location.href);
var strPath = window.location.href;
var path = strPath.substr(0,strPath.lastIndexOf('/')) + "/../../Documents/age.pdf";
console.log(encodeURI(path));
childbrowser.showWebPage(encodeURI(path));

这里的代码,什么打开pdf文件。文件位于本地文件夹 Documents

这是一个在Phonegap中如何为我工作的示例

url = "pdf/test.pdf";
function openChildBrowser(url)
{
   try {

   var strPath = window.location.href;
   var path = strPath.substr(0,strPath.lastIndexOf('www')) + "www/"+ url;
   path = path.replace('file:///', '/');
   window.plugins.childBrowser.showWebPage(encodeURI(path));

   }
   catch (err)
   {
    console.log(err);
   }

}

于 2012-10-27T09:55:15.007 回答