Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaver (PhoneGap / Cordova as component) not working in iOS

Tags:

ios

cordova

I tried to use Cleaver, that is PhoneGap / Cordova as a component of an existing iOS application. I followed this step by step guide twice, without success.

My application can easily instantiate a web view, and i can pass content to it using the stringbyevaluatingjavascriptfromstring method, but as soon as i try to access PhoneGap API (navigator.compass, navigator.network, ...), nothing seems to work. console.log(navigator) shows no sign of Cordova objects (console.log does not output anything is Xcode, i'm using http://debug.phonegap.com/). The deviceready event is not triggered either.

In my html file, i include PhoneGap js file cordova-1.7.0rc1.js which i copied from another project (since /www folder is missing when you use PhoneGap as a component).

My ViewController.h imports <Cordova/CDVViewController.h>. Here my 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

Do you have an idea of what's happening ? Any help would be greatly appreciated !

like image 236
Just Al Avatar asked Jun 29 '12 15:06

Just Al


1 Answers

Okay, after many hours of trying to solve this issue, I finally found what was going wrong. This is the line that messed things up:

cdvViewController.webView.delegate = self;

You can't just override the delegate of the view controllers's web view. The original delegate has instantiation code which enables PhoneGap to run. Don't override it and everything will be fine !

like image 87
Just Al Avatar answered Oct 12 '22 01:10

Just Al