1

I've been reading around but I didn't get to a solution yet. I'm downloading a pdf file with FileTransfer successfully. It is placed in a folder called Documents and the location looks like this: /Users/myusername/Library/Application Support/iPhone Simulator/5.1/Applications/C62E5802-56A8-48BF-B57C-695801F3C8D6/HelloWorld.app/Documents/11.pdf

I am trying to use ChildBrowser to open it and I had no success so far.

The code to open is this:

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

When path is external (e.g. http://www.google.com/) it works well (with the domain whitelisted).

I have tried all of these paths with no success:

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

file:///Documents/11.pdf

What do I need to do to get to it? Looking at the app location above, I cannot find the www folder either (I assume it has been packaged)

This is all in the simulator, cordova 2.1.0 and latest ChildBrowser .

NickOpris
  • 509
  • 2
  • 8
  • 20

3 Answers3

3

I found another question here that answers mine. link

The uri to file should be like this:

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

Providing the file has been downloaded into the Documents folder of the app and not placed in the www folder. Get the url like this:

fp = entry.fullPath;
urld = "file://" + fp;
//open the file in ChildBrowser
cordova.exec("ChildBrowserCommand.showWebPage", encodeURI(urld) );
Community
  • 1
  • 1
NickOpris
  • 509
  • 2
  • 8
  • 20
3

I have added a https://github.com/vfr/Reader PDF Reader to ChildBrowser. It works super great!

Current components version: Child Browser for Cordova 2.2.0

Edit the 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

IN THE .M FILE 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) Copy everything from the Reader to our projects, add all Frameworks needed

Enjoy. Don't forget to switch on the ARC for the Reader

Naloiko Eugene
  • 2,453
  • 1
  • 28
  • 18
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));

Here code, what open pdf file. file is in the local folder, Documents

Here is a example how it worked for me in 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);
   }

}

Community
  • 1
  • 1
dajver
  • 250
  • 1
  • 3
  • 22
  • this is attempting to open file:///Users/myusername/Library/Application Support/iPhone Simulator/5.1/Applications/C62E5802-56A8-48BF-B57C-695801F3C8D6/HelloWorld.app/www/../../Documents/11.pdf with no success. – NickOpris Oct 27 '12 at 11:21
  • addded file to local project folder Dosuments and open it as I have written in the code. I hate iOS for what they have here and so tangled all paths – dajver Oct 28 '12 at 10:06
  • I use FileTransfer so my file arrives after the app has been packaged. I cannot add the file to the project in advance. – NickOpris Oct 28 '12 at 10:31
  • unless I could download it in the www folder which I cannot find after packaging – NickOpris Oct 28 '12 at 10:33
  • i tried getting the url of the file like this var ur = entry.toURL(); then I placed that in a link. $('#link').html("link"); The file opens fine in same window. Any idea why it would not open in the ChildBrowser? – NickOpris Oct 28 '12 at 21:21
  • I'm commenting to my own comments but let's hope it helps someone: when I wanted links to open in the chidbrowser and they did not, it was because the js function had an error and exited prematurely allowing the click to go through and open the url in current browser. – NickOpris Jan 27 '13 at 19:43
  • Being in `www/` does not seem to be necessary: I managed to open a downloaded file in `Documents/` and show it in the childBrowser; had to sed `origin="*"` in the XML, though. And TEMPORARY path does not seem to work. – lapo Dec 16 '13 at 04:30
  • I'm also quite sure hydration kills it (I activated/de-activated/activated/de-activated and was broken/working/broken/working), I wonder why. – lapo Dec 16 '13 at 08:49
  • @NickOpris: toURL() is the way to go. try using js to open it up: window.open(ur, '_blank', 'location=yes'); – EeKay Apr 07 '14 at 10:40