2012-06-29 32 views
8

我尝试使用Cleaver,即PhoneGap/Cordova作为现有iOS应用程序的组件。我跟着this step by step guide两次,没有成功。Cleaver(PhoneGap/Cordova作为组件)在iOS中不工作

我的应用程序可以很容易地实例化一个Web视图,我可以使用stringbyevaluatingjavascriptfromstring方法传递的内容,但只要我尝试访问PhoneGap的API(navigator.compass, navigator.network, ...),似乎没有任何工作。 console.log(navigator)没有显示科尔多瓦对象的迹象(console.log不输出任何东西是Xcode,我正在使用http://debug.phonegap.com/)。 deviceready事件也没有被触发。

在我的html文件中,我包含PhoneGap js文件cordova-1.7.0rc1.js,这是我从另一个项目复制的(因为/www文件夹在使用PhoneGap作为组件时缺失)。我的ViewController.h进口<Cordova/CDVViewController.h>。 这里我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 

您有没有发生什么事的想法?任何帮助将不胜感激 !

回答

1

好吧,经过几个小时的试图解决这个问题,我终于发现出了什么问题。 这是把事情搞乱行:

cdvViewController.webView.delegate = self; 

你不能只是覆盖视图控制器的web视图的委托。原始代理具有使PhoneGap可以运行的实例化代码。不要重写它,一切都会好的!

+0

嗨,那么如果这条线没有添加,webViewDidFinishLoad函数不能被解雇? – red23jordan

0

如果你想收到通知的WebView事件不会破坏科尔多瓦你可以尝试这样的事:

@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 

用法是这样的:

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

我没有测试过这一点,所以你应该小心。