Skip to content

JavaScript bridge between native (iOS) and JavaScript

Notifications You must be signed in to change notification settings

montisan/WVJSBridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WVJSBridge

Version License Platform

Installation

WVJSBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "WVJSBridge"

Guide

Step 1:

    // Create `UIWebView` object, DONOT set webView.delegate.
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_webView];

    // Create WVJSBridge
    self.bridge = [[WVJSBridge alloc] initWithWebView:_webView bridgeName:@"hnBridge" delegate:self];

    // Register native methods for javascript to call
    [_bridge registerHandler:@"testObjcCallback" target:self selector:@selector(testObjcCallback:callback:)];

Step 2:

	// Call native method with javascript
    if(window.hnBridge)
    {
        ptBridge.testObjcCallback({
            // the params to delivery to the native
            data:{
                    param1:"parma1",
                    param2:"parma2"
            },
            callback:function(data){
                    // native callback function
            }
        });
    }

  	// Or:

    if(window.hnBridge)
    {
        ptBridge.send("testObjcCallback",{
            data:{
                    param1:"parma1",
                    param2:"parma2"
            },
            callback:function(data){
                    // native callback function
            }
        });
    }

Step 3:

    - (void)testObjcCallback:(id)data callback:(WVJBResponseCallback)cb
    {
        NSLog(@"Receive params(`NSDictionary` type) from javascript:%@",[data description]);
        if (cb)
        {
            // Return data to javascript
            cb(@{@"code":@(0)});

            // Return data to javascript asynchronously
            /*
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                // TO DO SOMETHINGS

                dispatch_async(dispatch_get_main_queue(), ^{
                    //Return data on main thread
                    cb(@{@"code":@(0)});
                });
            });
            */
        }
    }

Author

hncoder, [email protected]

License

WVJSBridge is available under the MIT license. See the LICENSE file for more info.

Notes

JavaScript bridge between native (iOS) and JavaScript, it modifies and optimizes the WebViewJavascriptBridge.

About

JavaScript bridge between native (iOS) and JavaScript

Resources

Stars

Watchers

Forks

Packages

No packages published