2013-10-07 33 views
0

我使用的PhoneGap 2.3 - 克利弗於iOS。iOS中使用PhoneGap的利斧覆蓋方法

我如何可以覆蓋shouldStartLoadWithRequest,webViewDidStartLoad,webViewDidFinishLoad功能?

如果我添加「viewController.webView.delegate =自我」到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 

回答

0

你不應該重寫shouldStartLoadWithRequest因爲他們管理的是調用本機功能的JavaScript網址。

或者至少複製從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調用

順便說一句,你不應該使用PhoneGap的2.3,你至少需要2.5通過5月加入蘋果商店的指導方針,以前的版本2.5中使用的UDID和它是由蘋果

禁止