2012-06-29 19 views
8

Tôi đã cố gắng sử dụng Cleaver, đó là PhoneGap/Cordova như một thành phần của một ứng dụng iOS hiện có. Tôi đã theo dõi this step by step guide hai lần mà không thành công.Cleaver (PhoneGap/Cordova là thành phần) không hoạt động trong iOS

Ứng dụng của tôi có thể dễ dàng khởi tạo chế độ xem web và tôi có thể chuyển nội dung sang chế độ xem bằng phương pháp stringbyevaluatingjavascriptfromstring, nhưng ngay khi tôi cố gắng truy cập API PhoneGap (navigator.compass, navigator.network, ...) thì không có gì có hiệu quả. console.log(navigator) cho thấy không có dấu hiệu của các đối tượng Cordova (console.log không đầu ra bất cứ điều gì là Xcode, tôi đang sử dụng http://debug.phonegap.com/). Sự kiện deviceready cũng không được kích hoạt.

Trong tệp html, tôi bao gồm tệp PhoneGap js cordova-1.7.0rc1.js mà tôi đã sao chép từ một dự án khác (vì thư mục /www bị thiếu khi bạn sử dụng PhoneGap làm thành phần).

My ViewController.h nhập <Cordova/CDVViewController.h>. Đây là 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 

Bạn có biết ý tưởng gì đang xảy ra không? Mọi sự trợ giúp sẽ rất được trân trọng !

Trả lời

1

Được rồi, sau nhiều giờ cố gắng giải quyết vấn đề này, cuối cùng tôi đã tìm thấy những gì đã xảy ra. Đây là dòng mà sai lầm mọi thứ lên:

cdvViewController.webView.delegate = self; 

Bạn không thể chỉ ghi đè lên các đại biểu của xem web xem các bộ điều khiển của. Người đại diện ban đầu có mã instantiation cho phép PhoneGap chạy. Đừng ghi đè lên nó và mọi thứ sẽ ổn thôi!

+0

Xin chào, sau đó nếu dòng này không được thêm vào, chức năng webViewDidFinishLoad không thể được kích hoạt? – red23jordan

0

Nếu bạn muốn nhận được thông báo về các sự kiện webview mà không vi phạm Cordova bạn có thể thử một cái gì đó như thế này:

@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 

Cách sử dụng như sau:

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

tôi đã không kiểm tra này, vì vậy bạn Nên cẩn thận.

Các vấn đề liên quan