PhoneGap/iOS,从.shouldStartLoadWithRequest()打开ChildBrowser?

问题描述:

我在PhoneGap中打包了一个移动网站(通过网络),并且想拦截指向PDF的链接并使用ChildBrowser插件打开它们。是否:可能从本机代码触发ChildBrowser(我已经确定要截取哪个链接)和:是AppDelegate.m,.shouldStartLoadWithRequest()是否是正确的位置?在这种情况下::如何从本机代码正确调用ChildBrowserPhoneGap/iOS,从.shouldStartLoadWithRequest()打开ChildBrowser?

我已经试过这固然幼稚的做法:

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

但只造成了沿...'NSInvalidArgumentException', reason: '-[AppDelegate exec:]: unrecognized selector sent to instance线的错误。

(PS:我知道这种做法是不理想的做法,但这个项目的价格仅2天的工作)

如果添加(Child Browser)的插件文件夹插件类,那么你必须与appDelegate.m文件播放,#import "ChildBrowserViewController.h"
例如HTML文件有以下HTML/JavaScript代码,这样
window.location="http://xyz.com/magazines/magazines101.pdf";
要在儿童浏览器中执行此网址,您需要修改URL请求本地shouldStartLoadWithRequest:方法,包含PDF扩展文件。


/** 
* 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 ];  
    } 
} 

感谢,
MAYUR

+0

意想不到的回答,非常感谢!奇迹般有效。 –

+0

@mayur你知道android的相同代码吗?我无法从原生android代码打开ChildBrowser。谢谢 – wakeup

任何寻找Android的等价物,把下面的代码里面shouldOverrideUrlLoading

ChildBrowser childBrowser = new ChildBrowser(); 
childBrowser.cordova = this; 
childBrowser.showWebPage(url, null);