Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap/iOS, open ChildBrowser from .shouldStartLoadWithRequest()?

I'm packaging a mobile website (over the network) in PhoneGap and would like to intercept links pointing to PDF's and open them using the ChildBrowser plugin. Is it 1: possible to trigger ChildBrowserfrom native code (I've already determined which links to intercept) and 2: is AppDelegate.m, .shouldStartLoadWithRequest() the right place to do it? And in that case: 3: how to correctly call ChildBrowser from native code?

I've tried this admittedly naive approach:

return [self exec:@"ChildBrowserCommand.showWebPage",
      [url absoluteString]];

but it only resulted in an error along the lines of ...'NSInvalidArgumentException', reason: '-[AppDelegate exec:]: unrecognized selector sent to instance.

(PS: I'm aware this approach isn't ideal practice, but this project is only priced for 2 days work)

like image 650
Jacob Oscarson Avatar asked May 25 '26 17:05

Jacob Oscarson


1 Answers

If you added (Child Browser) plugin classes in plugin folder then you have to play with the appDelegate.m file, #import "ChildBrowserViewController.h"
For example your html file has following html/javascript code, like this
window.location="http://xyz.com/magazines/magazines101.pdf";
To execute this url in Child Browser, you need to modify native shouldStartLoadWithRequest: method for request url which contain pdf extension files.


/**
 * Start Loading Request
 * This is where most of the magic happens... We take the request(s) and process the response.
 * From here we can re direct links and other protocalls to different internal methods.
 */
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    NSURL *url = [request URL];
    if([request.URL.absoluteString isEqualToString:@"about:blank"])
        return [ super webView:theWebView shouldStartLoadWithRequest:request
                navigationType:navigationType ];
    if ([[url scheme] isEqualToString:@"gap"]) {
        return [ super webView:theWebView shouldStartLoadWithRequest:request
                navigationType:navigationType ];
    } else {
        NSString *urlFormat = [[[url path] componentsSeparatedByString:@"."] lastObject];
        if ([urlFormat compare:@"pdf"] == NSOrderedSame) {
            [theWebView sizeToFit];
            //This code will open pdf extension files (url's) in Child Browser
            ChildBrowserViewController* childBrowser = [ [ ChildBrowserViewController alloc ] initWithScale:FALSE ];
            childBrowser.modalPresentationStyle = UIModalPresentationFormSheet;
            childBrowser.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;   
            [super.viewController presentModalViewController:childBrowser animated:YES ];   
            NSString* urlString=[NSString stringWithFormat:@"%@",[url absoluteString]]; 
            [childBrowser loadURL:urlString];
            [childBrowser release];
            return NO;      
        } 
        else
            return [ super webView:theWebView shouldStartLoadWithRequest:request
                    navigationType:navigationType ];    
    } 
}

thanks,
Mayur

like image 106
Mayur Birari Avatar answered May 27 '26 06:05

Mayur Birari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!