This library provides a basic framework for iOS hybrid development.
It provides:
-
Template resource(html/js/css) management.
-
Bridge for coding with JS and Objc, using JavaScriptCore.Framework.
- send message from JavaScript to native, and callback after native responsed.
The data flows: Javascript --> JavaScriptCore --> Native --> JavaScriptCore --> Javascript
- Call JavaScript function in native, and callback after Javascript responsed.
The data flows: Native --> JavaScriptCore --> JavaScript --> JavaScriptCore --> Native
- Native send message to JavaScript, such as pushing notification.
With this library, you can communicate your native codes with web codes easily.
Hybrid apps are part native apps, part web apps. Like native apps, they live in an app store and can take advantage of the many device features available. They rely on HTML being rendered in UIWebViews, so as to avoid publish new versions for some updates. More details in this Blog.
- Grab the source file into your project.
- Or use cocoapods. CocoaPods is really great. Here is an example of your podfile:
pod 'HybridBridge'
Inherite BridgeWebViewController for your custom controller
@interface MainViewController : BridgeWebViewController
And now you can communicate between js and objc.
There are two ways for JavaScript to drive to communication.
- Send message and callback
In your js code, you can use sendMessageAndCallback to send message from js to objc and callback after the send operation succeeds.
bridge.sendMessageAndCallback(eventType, message, callbackFunction)
On the other hand, native can call JavaScript codes. After you register handler in JavaScript, you can call this handler in native side now.
JavaScript register handler:
bridge.registerHandler(handlerName, handler)
Native call handler:
[self callHandler:@"handler" parameters:@[] callback:^(id responseData) {
// process with response data from js
}];
Native push notification:
[self sendMessageToJSForKey:eventType value:messages];
JavaScript receive message:
bridge.receiveMessage(eventType, callback)
- Push ViewController.
bridge.pushController(url, name)
The name is used to route controller in navite end.
- Get JSON:
bridge.getJSON(url, options, callback)
In hybrid app, in order to improve the performance, we always hold the template resources(html/js/css/img) locally.
Firstly, create initial resources for the first time in AppDelegate:
#import "WebBridgeAPI.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
ResourceManager *manager = [ResourceManager sharedManager];
[manager createInitialResource];
return YES;
}
Check resource update and download if needed:
For more details, please check the example project.
欢迎关注简书,关注微信公众号(iOSers),订阅高质量原创技术文章: