2012-06-29 34 views
8

He intentado usar Cleaver, es decir PhoneGap/Cordova como un componente de una aplicación iOS existente. Seguí this step by step guide dos veces, sin éxito.Cleaver (PhoneGap/Cordova como componente) no funciona en iOS

Mi aplicación puede instanciar fácilmente una vista web, y puedo pasarle contenido usando el método stringbyevaluatingjavascriptfromstring, pero tan pronto como intento acceder a PhoneGap API (navigator.compass, navigator.network, ...), nada parece funcionar. console.log(navigator) no muestra signos de objetos Cordova (console.log no muestra nada es Xcode, estoy usando http://debug.phonegap.com/). El evento deviceready tampoco se desencadena.

En mi archivo html, incluyo el archivo PhoneGap js cordova-1.7.0rc1.js que copié de otro proyecto (ya que falta la carpeta /www cuando usa PhoneGap como componente).

Mi ViewController.h importaciones <Cordova/CDVViewController.h>. Aquí mi ViewController.m

// 
// ViewController.m 
// CordovaComponent 
// 

#import "ViewController.h" 

@interface ViewController(){ 
    CDVViewController *cdvViewController ; 
} 
@end 

@implementation ViewController 

- (void)pushPhoneGap:(id)sender { 

    cdvViewController = [[CDVViewController alloc] init]; 
    cdvViewController.wwwFolderName = @"www"; 
    cdvViewController.startPage = @"index.html"; 
    cdvViewController.view.frame = self.view.bounds; 
    cdvViewController.webView.delegate = self; 
    [self.navigationController pushViewController:cdvViewController animated:YES]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setTitle:@"Push PhoneGap view" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(pushPhoneGap:) forControlEvents:UIControlEventTouchUpInside]; 
    button.frame = CGRectMake(60, 50, 200., 50.); 
    [self.view addSubview:button]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
    NSString *jsReturn = [cdvViewController.webView stringByEvaluatingJavaScriptFromString:@"setArray([1, 1, 2, 3, 5, 8, 13, 21, 34, 1]);"]; 
    NSLog(jsReturn); 
} 

@end 

¿Tiene usted una idea de lo que está pasando? ¡Cualquier ayuda sería muy apreciada!

Respuesta

1

Bien, después de muchas horas de tratar de resolver este problema, finalmente encontré lo que estaba pasando mal. Esta es la línea que las cosas en mal estado:

cdvViewController.webView.delegate = self; 

No se puede anular el delegado de vista Web de la controladores de vista. El delegado original tiene un código de instanciación que permite ejecutar PhoneGap. ¡No lo anule y todo estará bien!

+0

Hola, si esta línea no se agrega, la función webViewDidFinishLoad no se puede activar? – red23jordan

0

Si desea para ser notificado sobre eventos WebView sin romper Cordova usted podría intentar algo como esto:

@interface WrappingWebViewDelegate : NSObject <UIWebViewDelegate> 
    @property (nonatomic,retain) id<UIWebViewDelegate> wrappedDelegate; 
@end 


@implementation WrappingWebViewDelegate 
    - (void)webViewDidStartLoad:(UIWebView*)theWebView { 
     [self.wrappedDelegate webViewDidStartLoad: theWebView]; 
     // your code 
    } 

    // implement all other delegate methods and forward them to the wrapped delegate 
    [...] 
@end 

Uso de la siguiente manera:

cdvViewController = [[CDVViewController alloc] init]; 
WrappingWebViewDelegate wrappingDelegate = [[WrappingWebViewDelegate alloc] init]; 
wrappingDelegate.wrappedDelegate = cdvViewController.webView.delegate; 
cdvViewController.webView.delegate = wrappingDelegate; 

no he probado esto, por lo que debe tener cuidado

Cuestiones relacionadas