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

Jac*_*son 3 iphone objective-c ios phonegap-plugins cordova

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

我尝试过这种天真的方法:

return [self exec:@"ChildBrowserCommand.showWebPage",
      [url absoluteString]];
Run Code Online (Sandbox Code Playgroud)

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

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

May*_*ari 7

如果您在插件文件夹中添加(子浏览器)插件类,那么您必须使用appDelegate.m文件,#import "ChildBrowserViewController.h"
例如您的html文件具有以下html/javascript代码,如下所示
window.location="http://xyz.com/magazines/magazines101.pdf";
要在子浏览器中执行此URL,您需要修改shouldStartLoadWithRequest:请求URL的本机方法,其中包含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 ];    
    } 
}
Run Code Online (Sandbox Code Playgroud)

谢谢,
Mayur