【问题标题】:Cleaver (PhoneGap / Cordova as component) not working in iOSCleaver(PhoneGap / Cordova 作为组件)在 iOS 中不起作用
【发布时间】:2012-06-29 15:40:48
【问题描述】:

我尝试使用 Cleaver,即 PhoneGap / Cordova 作为现有 iOS 应用程序的组件。我关注了this step by step guide 两次,都没有成功。

我的应用程序可以轻松地实例化一个 Web 视图,并且我可以使用 stringbyevaluatingjavascriptfromstring 方法将内容传递给它,但是当我尝试访问 PhoneGap API (navigator.compass, navigator.network, ...) 时,似乎没有任何效果。 console.log(navigator) 没有显示 Cordova 对象的迹象(console.log 不输出任何东西是 Xcode,我正在使用 http://debug.phonegap.com/)。 deviceready 事件也未触发。

在我的 html 文件中,我包含了 PhoneGap js 文件 cordova-1.7.0rc1.js,它是我从另一个项目中复制的(因为当您使用 PhoneGap 作为组件时,/www 文件夹丢失了)。

我的 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

你知道发生了什么吗?任何帮助将不胜感激!

【问题讨论】:

    标签: ios cordova


    【解决方案1】:

    好的,经过几个小时的尝试解决这个问题,我终于找到了问题所在。 这是把事情搞砸的那一行:

    cdvViewController.webView.delegate = self;
    

    您不能只覆盖视图控制器的 Web 视图的委托。原始委托具有使 PhoneGap 能够运行的实例化代码。不要覆盖它,一切都会好起来的!

    【讨论】:

    • 你好,那如果不加这行,是不是不能触发webViewDidFinishLoad函数呢?
    【解决方案2】:

    如果您想在不破坏 Cordova 的情况下获得有关 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;
    

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

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      相关资源
      最近更新 更多