Git Product home page Git Product logo

Comments (11)

marcuswestin avatar marcuswestin commented on June 21, 2024

Hi,

Sure you can! Make it @interface CurrentTimeViewController : UIViewController <WebViewJavascriptBridgeDelegate>, and then implement - (void) handleMessage:(NSString*) message fromWebView: (UIWebView *)theWebView; in the view controller.

Posting more of your code would make it easier to help you.

Also, are you sure the issue is in the WebViewJavascriptBridge code? Maybe you could start with the provided example and then start modifying your code from there?

Cheers!

from webviewjavascriptbridge.

imranansari avatar imranansari commented on June 21, 2024

Thanks for the response, When I use your example code as is, and hook in my web page, it works fine and I can invoke commands both ways, but below is my interface and implementation using a UIController with a tab nav, please have a look.

#import <Foundation/Foundation.h>
#import "WebViewJavascriptBridge.h"


@interface CurrentTimeViewController : UIViewController <WebViewJavascriptBridgeDelegate>

@property (strong, nonatomic) WebViewJavascriptBridge *javascriptBridge;
@property (strong, nonatomic) UIWebView *webView;

@end

Impl

#import "CurrentTimeViewController.h"

@implementation CurrentTimeViewController

@synthesize javascriptBridge;
@synthesize webView;

- (id)init
{
    // Call the superclass's designated initializer
    self = [super initWithNibName:nil
                           bundle:nil];
    if (self) {
        // Get the tab bar item
        UITabBarItem *tbi = [self tabBarItem];

        [tbi setTitle:@"Search"];
    }

    return self;
}

- (void)viewDidLoad
{

    NSLog(@"Loaded the view for CurrentTime controller");
    [super viewDidLoad];

    webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:4567/form"]]];

    javascriptBridge = [WebViewJavascriptBridge javascriptBridge];
    javascriptBridge.delegate = self;
    webView.delegate = javascriptBridge;

    [javascriptBridge sendMessage:@"HI" toWebView:webView];

    [self.view addSubview:webView];
    [webView release];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {


}

- (void)handleMessage:(NSString *)message fromWebView:(UIWebView *)theWebView {
    NSLog(@"ExampleWebViewJavascriptBridgeDelegate received message: %@", message);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"JSON passed from WebView" 
                                                    message: message 
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}


@end

from webviewjavascriptbridge.

marcuswestin avatar marcuswestin commented on June 21, 2024

Cool! And on what line does the exception happen?

-- while mobile

On Nov 28, 2011, at 6:39 PM, Imran [email protected] wrote:

Thanks for the response, When I use your example code as is, and hook in my web page, it works fine and I can invoke commands both ways, but below is my interface and implementation using a UIController with a tab nav, please have a look.

#import <Foundation/Foundation.h>
#import "WebViewJavascriptBridge.h"


@interface CurrentTimeViewController : UIViewController <WebViewJavascriptBridgeDelegate>

@property (strong, nonatomic) WebViewJavascriptBridge *javascriptBridge;
@property (strong, nonatomic) UIWebView *webView;

@end

Impl

#import "CurrentTimeViewController.h"

@implementation CurrentTimeViewController

@synthesize javascriptBridge;
@synthesize webView;

- (id)init
{
   // Call the superclass's designated initializer
   self = [super initWithNibName:nil
                          bundle:nil];
   if (self) {
       // Get the tab bar item
       UITabBarItem *tbi = [self tabBarItem];

       [tbi setTitle:@"Search"];
   }

   return self;
}

- (void)viewDidLoad
{

   NSLog(@"Loaded the view for CurrentTime controller");
   [super viewDidLoad];

   webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 
   [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:4567/form"]]];

   javascriptBridge = [WebViewJavascriptBridge javascriptBridge];
   javascriptBridge.delegate = self;
   webView.delegate = javascriptBridge;

   [javascriptBridge sendMessage:@"HI" toWebView:webView];

   [self.view addSubview:webView];
   [webView release];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {


}

- (void)handleMessage:(NSString *)message fromWebView:(UIWebView *)theWebView {
   NSLog(@"ExampleWebViewJavascriptBridgeDelegate received message: %@", message);

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"JSON passed from WebView" 
                                                   message: message 
                                                  delegate:nil 
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
   [alert show];
}


@end

Reply to this email directly or view it on GitHub:
#3 (comment)

from webviewjavascriptbridge.

imranansari avatar imranansari commented on June 21, 2024

I get a EXC_BAD_ACCESS in the return statement below.

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

from webviewjavascriptbridge.

marcuswestin avatar marcuswestin commented on June 21, 2024

Well, I can help you much easier if you track down which line in your application code is causing the error. Help me help you a bit here :)

For example, add a bunch of NSLog statements in your application unit initialization code - when you get to a log statement that didn't execute, one of the lines before it will likely be your culprit!

-- while mobile

On Nov 28, 2011, at 7:09 PM, Imran [email protected] wrote:

I get a EXC_BAD_ACCESS in the return statement below.

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
   @autoreleasepool {
       return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
   }
}

Reply to this email directly or view it on GitHub:
#3 (comment)

from webviewjavascriptbridge.

imranansari avatar imranansari commented on June 21, 2024

Let me run through the debugger again, and come back to you on what I find, I havent tried debuggigng the AppDelegate yet.

Just downloaded 13 hrs worth of ObjectiveC from iDeveloper.tv, hope that helps, hate being a newb :)

from webviewjavascriptbridge.

marcuswestin avatar marcuswestin commented on June 21, 2024

Totally - enjoy!

I'm fairly new to iOS development myself so I may not be the best person to ask.

You'll probably find that some of your app delegate code is getting executed, and that somewhere down that stack there is a bad reference to something that haven't been alloced or something like that.

Cheers,
Marcus

-- while mobile

On Nov 28, 2011, at 7:25 PM, Imran [email protected] wrote:

Let me run through the debugger again, and come back to you on what I find, I havent tried debuggigng the AppDelegate yet.

Just downloaded 13 hrs worth of ObjectiveC from iDeveloper.tv, hope that helps, hate being a newb :)


Reply to this email directly or view it on GitHub:
#3 (comment)

from webviewjavascriptbridge.

marcuswestin avatar marcuswestin commented on June 21, 2024

How did this work out for you?

from webviewjavascriptbridge.

imranansari avatar imranansari commented on June 21, 2024

I couldn't, below is the link to the example app I was trying, see if you can figure out why it crashes, I couldn't even debug it.

https://github.com/imranansari/Tabstarter1

thanks for your help.
Imran

On Dec 2, 2011, at 3:38 PM, Marcus Westin wrote:

How did this work out for you?


Reply to this email directly or view it on GitHub:
#3 (comment)

from webviewjavascriptbridge.

hpoul avatar hpoul commented on June 21, 2024

i had a similar problem, but adding [callback copy] to registerObjcCallback worked (for now)

- (void)registerObjcCallback:(NSString *)name withCallback:(void (^)(NSDictionary *params))callback {
    [self.javascriptCallbacks setObject:[callback copy] forKey:name];
}

anyone can explain that? :) someone on stackoverflow posted that on a question about ios callbacks: http://stackoverflow.com/a/3915774/109219

    // NOTE: copying is very important if you'll call the callback asynchronously,
    // even with garbage collection!

from webviewjavascriptbridge.

marcuswestin avatar marcuswestin commented on June 21, 2024

@hpoul Added your change in 287384d. Also, see http://stackoverflow.com/questions/4664804/why-does-the-assignment-of-an-objc-block-have-to-be-copy-not-assign

from webviewjavascriptbridge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.