1

I am developing a hybrid application for iOS. In order to achieve two way communication between Javascript and the native code, I am using Webviewjavascriptbridge, a third-party library. This is the code that I am using.

//pkViewcontroller.h
@class WebViewJavascriptBridge;
@property (strong, nonatomic) WebViewJavascriptBridge *javascriptBridge;  
@property (strong, nonatomic) IBOutlet UIWebView *webView;
//pkViewcontroller.m
 - (void)viewDidLoad
 {
     [WebViewJavascriptBridge enableLogging];
     _bridge= [WebViewJavascriptBridge bridgeForWebView:_webView handler:nil];
     NSString *path=[[NSBundle mainBundle]pathForResource:@"index"  ofType:@"html"];
     NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL  fileURLWithPath:path]];
     [_webView loadRequest:request];

    [_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponse *response) {
    NSLog(@"after getContactBttnTapped method call");
    ABPeoplePickerNavigationController *picker=[[ABPeoplePickerNavigationController alloc]init];
    picker.peoplePickerDelegate= self;
    [self presentViewController:picker animated:YES completion:nil];
   }];
}

HTML file

<html>
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="test.js"></script>
  </head>
  <body>
    <input type="Button" value="ChooseContact" onClick="ChooseContactTapped()">
  </body>
</html>

test.js

var bridge
window.onerror = function(err) {
  alert('window.onerror: ' + err)
}
document.addEventListener('WebViewJavascriptBridgeReady', onBridgeReady, false)
function onBridgeReady(event) {
  bridge = event.bridge
}

function ChooseContactTapped(){
  bridge.callHandler('testObjcCallback', {'foo': 'bar'}, function(response) {
    log('Got response from testObjcCallback', response)
  })
}

The JS file will be residing on the server. Now I want to call my native method without writing the code inside the JS file. Please help me. If what I am doing is not correct, please suggest me a way to make the communication between native code and Javascript. Please note that the JS file will be on the server and also I need to pass objects between JS and the native code.

dda
  • 6,030
  • 2
  • 25
  • 34
user1878200
  • 87
  • 1
  • 3
  • Check this link http://stackoverflow.com/questions/1662473/how-to-call-objective-c-from-javascript – Anoop Vaidya Dec 05 '12 at 08:02
  • @anoop if i use that way, i hope i cannot pass the object from js to objective C. if i can pass the object.Please let me know the way. – user1878200 Dec 05 '12 at 08:05
  • Create a Data class(a prototype) in objective-c, and get all the properties from your server. Fill that data class. – Anoop Vaidya Dec 05 '12 at 08:14
  • @anoop Sorry i did not understand. Can you please explain in detail. – user1878200 Dec 05 '12 at 08:23
  • It is quite task, but you can attain what you want to do, actually same thing I did a year ago. I don't own the code as company policy. But I remember the idea, which is as: Call a service and you can send all objects' contents in xml. Parse that xml and form a data class. Build a Data class from that xml. In the end you will have a copy of Object in cocoa that you wish to pass from server using JS. – Anoop Vaidya Dec 05 '12 at 09:08
  • @anoop Thanks for your reply, let me try something like this – user1878200 Dec 05 '12 at 10:58

1 Answers1

0

As per our discussion,

Here is the way to solve this problem.

It is quite task, but you can attain what you want to do.

-Call a service and you can send all objects' contents in xml.

-Parse that xml and form a data class.

-Build a Data class from that xml.

-In the end you will have a copy of Object in cocoa that you wish to pass from server using JS

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140