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

Trouble getting this to work in a UIViewController #65

Closed
Theorian opened this issue Jan 11, 2014 · 6 comments
Closed

Trouble getting this to work in a UIViewController #65

Theorian opened this issue Jan 11, 2014 · 6 comments

Comments

@Theorian
Copy link

I tried this from what I read in some other issues. But there seems to be no protocol defined.

@interface MyViewController : UIViewController

So I tried:

@interface MyViewController : UIViewController

this still doesn't do anything. Following your instruction worked fine from my appdelegate. but whenever I bring it over to a viewcontroller it doesn't even log any output

@Theorian
Copy link
Author

Can anyone please offer some advice on getting this to work with UIViewController?

@tanis2000
Copy link

Could you please provide some more context about this issue so that we can
try and help you?

@Theorian
Copy link
Author

I'm having trouble thinking of what else I can offer as far as context. I would definitely explain more if I knew what else to provide. Is it possible for you to try and use Marcus' instructions from the README inside a UIViewController. https://github.com/marcuswestin/WebViewJavascriptBridge#setup--examples-ios--osx maybe I can post some code.

MyViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/CoreAnimation.h>
#import "WebViewJavascriptBridge.h"
@interface MyViewController : UIViewController
@EnD

MyViewController.m
#import "MyViewController.h"

@interface MyViewController()

  • (NSUInteger) screenNumber;
    @EnD

@implementation MyViewController

  • (void)viewDidAppear:(BOOL)animated {

    self.view.backgroundColor = [UIColor whiteColor];

    NSUInteger screenNumber = [self screenNumber];

    NSLog(@"screen: %lu", (unsigned long)screenNumber);

    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

    if (screenNumber == 1){
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"stopwatch" ofType:@"html" inDirectory:@"/stopwatch"]];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    } else {
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"watchdisplay" ofType:@"html" inDirectory:@"/stopwatch"]];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    }

    [self.view addSubview:webView];

    WebViewJavascriptBridge *_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
    NSLog(@"ObjC received message from JS: %@", data);
    responseCallback(@"Response for message from ObjC");
    }];

    [_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
    NSLog(@"testObjcCallback called: %@", data);
    responseCallback(@"Response from testObjcCallback");
    }];

    [_bridge send:@"A string sent from ObjC before Webview has loaded." responseCallback:^(id responseData) {
    NSLog(@"objc got response! %@", responseData);
    }];

    [_bridge callHandler:@"testJavascriptHandler" data:@{ @"foo":@"before ready" }];

    [_bridge send:@"A string sent from ObjC after Webview has loaded."];
    }

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

  • (void) handleMessage:(NSString*) message fromWebView: (UIWebView *)theWebView{
    }

  • (NSUInteger) screenNumber {
    NSUInteger result = 1;
    UIWindow *_window = nil;
    UIScreen *_screen = nil;
    NSArray *_screens = nil;

    _screens = [UIScreen screens];

    if ([_screens count] > 1){
    _window = [[self view] window];
    _screen = [_window screen];
    if (_screen != nil){
    for(size_t i = 0; i < [_screens count]; ++i){
    UIScreen *_currentScreen = [_screens objectAtIndex:i];
    if (_currentScreen == _screen){
    result = i+1;
    }
    }
    }
    }
    return result;
    }
    @EnD

@marcuswestin
Copy link
Owner

Hi @Theorian,

I updated the repo's example iOS app to use a UIViewController. That should give you a working example to build off of.

Cheers!
Marcus

@Theorian
Copy link
Author

Thank you so much :)

@tanis2000
Copy link

BTW the error seems to be the way you instantiate the bridge. You're doing this, which requires a delegate and your class to implement it:

WebViewJavascriptBridge *_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC received message from JS: %@", data);
responseCallback(@"Response for message from ObjC");
}];

Instead, you should use the other form of instantiation that does not use a delegate, like detailed in the instruction by Marcus:

WebViewJavascriptBridge* bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) {
    NSLog(@"Received message from javascript: %@", data);
    responseCallback(@"Right back atcha");
}];

Cheers!

Valerio

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