0

我正在使用 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
4

1 回答 1

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;
}

管理phonegapif ([[url scheme] isEqualToString:@"gap"])通话

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

于 2013-11-14T13:45:24.830 回答