Git Product home page Git Product logo

Comments (6)

Theorian avatar Theorian commented on June 21, 2024

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

from webviewjavascriptbridge.

tanis2000 avatar tanis2000 commented on June 21, 2024

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

from webviewjavascriptbridge.

Theorian avatar Theorian commented on June 21, 2024

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

from webviewjavascriptbridge.

marcuswestin avatar marcuswestin commented on June 21, 2024

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

from webviewjavascriptbridge.

Theorian avatar Theorian commented on June 21, 2024

Thank you so much :)

from webviewjavascriptbridge.

tanis2000 avatar tanis2000 commented on June 21, 2024

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

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.