3

我正在PhoneGap 中打包一个移动网站(通过网络),并希望拦截指向PDF 的链接并使用ChildBrowser插件打开它们。1 :可以从本机代码触发ChildBrowser(我已经确定要拦截哪些链接)和2:是AppDelegate.m正确.shouldStartLoadWithRequest()的地方吗?在那种情况下:3:如何正确地ChildBrowser从本机代码调用?

我已经尝试过这种公认的幼稚方法:

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

但它只会导致错误...'NSInvalidArgumentException', reason: '-[AppDelegate exec:]: unrecognized selector sent to instance

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

4

2 回答 2

7

如果您在插件文件夹中添加(子浏览器)插件类,那么您必须使用 appDelegate.m 文件,#import "ChildBrowserViewController.h"
例如您的 html 文件具有以下 html/javascript 代码,如下所示
window.location="http://xyz.com/magazines/magazines101.pdf";
要在子浏览器中执行此 url,您需要修改shouldStartLoadWithRequest:包含 pdf 扩展文件的请求 url 的本机方法。


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

谢谢,
马尤尔

于 2011-10-31T11:25:55.010 回答
0

任何寻找 android 等效项的人,将下面的代码放入shouldOverrideUrlLoading

ChildBrowser childBrowser = new ChildBrowser();
childBrowser.cordova = this;
childBrowser.showWebPage(url, null);
于 2013-08-01T06:49:33.610 回答