我正在 PhoneGap 中打包一个移动网站(通过网络),并想拦截指向 PDF 的链接并使用 ChildBrowser 插件打开它们。 1 :可以从 native 代码触发 ChildBrowser (我已经确定要拦截哪些链接)和 2 :AppDelegate.m , .shouldStartLoadWithRequest() 是正确的地方吗?在这种情况下: 3 :如何从 native 代码正确调用 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,您需要修改包含 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 ];
    }
}
谢谢,
马尤尔

关于iphone - PhoneGap/iOS,从 .shouldStartLoadWithRequest() 打开 ChildBrowser?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7952626/

10-10 17:55