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

Posted

技术标签:

【中文标题】PhoneGap/iOS,从 .shouldStartLoadWithRequest() 打开 ChildBrowser?【英文标题】:PhoneGap/iOS, open ChildBrowser from .shouldStartLoadWithRequest()? 【发布时间】:2011-10-31 10:45:09 【问题描述】:

我正在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 天的工作定价)

【问题讨论】:

【参考方案1】:

如果您在插件文件夹中添加了 (Child Browser) 插件类,那么您必须使用 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 ];    
     

谢谢, 马尤尔

【讨论】:

@mayur 你知道 android 的相同代码吗?我无法从原生 android 代码打开 ChildBrowser。谢谢【参考方案2】:

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

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

【讨论】:

以上是关于PhoneGap/iOS,从 .shouldStartLoadWithRequest() 打开 ChildBrowser?的主要内容,如果未能解决你的问题,请参考以下文章

PHONEGAP - 无法导入证书 - IOS

如何从 PhoneGap iOS 6 应用程序中删除位置服务请求?

如何在phonegap ios中将设备令牌从Xcode抓取到javascript?

如何从 Phonegap iOS 项目中删除未使用的框架和库?

使用 phonegap(ios) 调用 Web 服务

PhoneGap iOS9黑边问题