在iOS中使用PhoneGap Cleaver覆盖方法

时间:2013-10-07 06:27:13

标签: ios cordova uiwebview uiwebviewdelegate

我正在使用PhoneGap 2.3 - 适用于iOS的Cleaver。

如何覆盖shouldStartLoadWithRequest,webViewDidStartLoad,webViewDidFinishLoad函数?

如果我将“viewController.webView.delegate = self”添加到viewDidLoad,可以调用上面的函数,但不能调用PhoneGap API。

感谢。

MyViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CDVViewController* viewController = [CDVViewController new];
    viewController.view.frame = self.view.bounds;
    //viewController.webView.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:landingURL]];
    [viewController.webView loadRequest:request];
    [self.view addSubview:viewController.view];
    [self addChildViewController:viewController];
}

MyViewController.h:

@interface MyViewController : UIViewController <UIWebViewDelegate>
@end

1 个答案:

答案 0 :(得分:0)

你不应该覆盖shouldStartLoadWithRequest,因为他们在那里管理调用本机函数的javascript url。

或者至少将他们的代码从CDVViewController复制到您的视图控制器中并添加您需要的东西。

示例来自phonegap 2.9.1

    - (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL* url = [request URL];

    /*
     * Execute any commands queued with cordova.exec() on the JS side.
     * The part of the URL after gap:// is irrelevant.
     */
    if ([[url scheme] isEqualToString:@"gap"]) {
        [_commandQueue fetchCommandsFromJs];
        return NO;
    }

    /*
     * If a URL is being loaded that's a file/http/https URL, just load it internally
     */
    else if ([url isFileURL]) {
        return YES;
    }

    /*
     *    If we loaded the HTML from a string, we let the app handle it
     */
    else if (self.loadFromString == YES) {
        self.loadFromString = NO;
        return YES;
    }

    /*
     * all tel: scheme urls we let the UIWebview handle it using the default behavior
     */
    else if ([[url scheme] isEqualToString:@"tel"]) {
        return YES;
    }

    /*
     * all about: scheme urls are not handled
     */
    else if ([[url scheme] isEqualToString:@"about"]) {
        return NO;
    }

    /*
     * all data: scheme urls are handled
     */
    else if ([[url scheme] isEqualToString:@"data"]) {
        return YES;
    }

    /*
     * Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
     */
    else {
        if ([self.whitelist schemeIsAllowed:[url scheme]]) {
            return [self.whitelist URLIsAllowed:url];
        } else {
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                [[UIApplication sharedApplication] openURL:url];
            } else { // handle any custom schemes to plugins
                [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
            }
        }

        return NO;
    }

    return YES;
}

if ([[url scheme] isEqualToString:@"gap"])管理手机拨号电话

顺便说一下,你不应该使用phonegap 2.3,你需要至少2.5才能通过苹果商店指南中添加的可能,2.5之前的版本使用了UDID并且它被苹果禁止