Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can this be used with a UIViewController? #3

Closed
imranansari opened this issue Nov 28, 2011 · 11 comments
Closed

Can this be used with a UIViewController? #3

imranansari opened this issue Nov 28, 2011 · 11 comments

Comments

@imranansari
Copy link

Hi,

I'm trying to build a Hybrid app using the bridge (I'm literally a few days old in iOS dev) .

have a tabbar navigation based app, and I'm using a UIViewController for each tab view, one of the tab would use the WebView.
I wast sure if I could implement WebViewJavascriptBridgeDelegate in a UIViewController class like below.

@interface CurrentTimeViewController : UIViewController <UIWebViewDelegate, WebViewJavascriptBridgeDelegate>

I get a EXC_BAD_ACC during runtime in the main class

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

Any pointer would be much appreciated.
-Imran

@marcuswestin
Copy link
Owner

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!

@imranansari
Copy link
Author

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

@marcuswestin
Copy link
Owner

Cool! And on what line does the exception happen?

-- while mobile

On Nov 28, 2011, at 6:39 PM, Imran Ansarireply@reply.github.com 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)

@imranansari
Copy link
Author

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]));
    }
}

@marcuswestin
Copy link
Owner

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 Ansarireply@reply.github.com 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)

@imranansari
Copy link
Author

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 :)

@marcuswestin
Copy link
Owner

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 Ansarireply@reply.github.com 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)

@marcuswestin
Copy link
Owner

How did this work out for you?

@imranansari
Copy link
Author

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)

@hpoul
Copy link

hpoul commented Aug 7, 2012

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!

@marcuswestin
Copy link
Owner

marcuswestin pushed a commit that referenced this issue Jun 14, 2016
Edited README example to make sure that the JavaScript callHandler (in #5) is able to send data to the objective-c registerHandler (in #3).  This is for the handler "ObjC Echo."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants