繁体   English   中英

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

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

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

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

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

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

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

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


/**
 * 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

任何寻找android等价物的人,将下面的代码放在shouldOverrideUrlLoading中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM