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

时间:2011-10-31 10:45:10

标签: iphone objective-c ios cordova phonegap-plugins

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

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

ChildBrowser

但它只会导致return [self exec:@"ChildBrowserCommand.showWebPage", [url absoluteString]]; 的错误。

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

2 个答案:

答案 0 :(得分:7)

如果你在插件文件夹中添加了(Child Browser)插件类,那么你必须使用appDelegate.m文件,#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

答案 1 :(得分:0)

任何寻找Android等效的人,将下面的代码放在 shouldOverrideUrlLoading

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