www.priyaontech.com
Native-JS Bridging
Using WKWebViews
Priya Rajagopal
@rajagp
www.priyaontech.com
WebKit
• Rendering/Layout Engine for web content
• Powers Safari & web views (Native Apps)
• JS Processing
• HTML/CSS rendering
• Networking/ Web request response Handling
www.priyaontech.com
Pre iOS8
• UIWebViews
• UIKit.framework
• In-process loading of web content
• Limited
• No JS Nitro optimizations
• Many WebKit capabilities not exposed
• Bridging between Native and JS a pain…
iOS 8
www.priyaontech.com
Modern WebKit
• Unified WebKit Framework across OS X 10.10 & iOS 8
• Safari & Native Apps
• Multi-process Architecture
• web page loaded in a separate process(upto a limit)
• Hardware Accelerated Scrolling (60 fps)
• JS Nitro Engine
• Built-in Navigation Gestures (swipe back/forward, zoom…)
www.priyaontech.com
Modern WebKit
• Simplified Native <->JS Communication
www.priyaontech.com
WKWebView
Replaces UIWebView (iOS)
WKWebView
WKNavigationDelegate
WKUIDelegate
WKBackForwardList
…
WebKit.framework
WKWebViewConfiguration
WKUserContentController
WKPreferences
WKProcessPool
allowsInlineMediaPlayback
mediaPlaybackAllowsAirPlay
….
- (instancetype)initWithFrame:(CGRect)frame configuration:
(WKWebViewConfiguration *)configuration
www.priyaontech.com
Native->JS
• User Scripts (WKUserScript)
• Inject JS into web page that …
• Registers For Events
• Customizes web page behavior
• Calls back into Native
www.priyaontech.com
User Scripts
// Setup WKUserContentController instance
WKUserContentController* userController = [[WKUserContentController
alloc]init];
// Specify when and where and what user script needs to be injected into
the web document
WKUserScript* userScript = [[WKUserScript alloc]initWithSource:myScript
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
// Add the user script to the WKUserContentController instance
[userController addUserScript:userScript];
// Configure the WKWebViewConfiguration instance with the
WKUserContentController
_webConfig.userContentController = userController;
www.priyaontech.com
JS Evaluation
-
- (void)evaluateJavaScript:(NSString *)javaScriptString
completionHandler:(void (^)(id, NSError *))completionHandler;
www.priyaontech.com
JS->Native
• Script Messages
window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
• Register Script Message Handler in Native
Implement WKScriptMessageHandler Protocol
• JS Objects Auto mapped to Obj-C / Swift
Objects
www.priyaontech.com
Script Messages
// Add a script message handler for receiving "MyEvent" event notifications
posted from the JS document using
window.webkit.messageHandlers.MyEvent.postMessage script message
[userController addScriptMessageHandler:self name:@"MyEvent"];
// Implement WKScriptMessageHandler Protocol
- (void)userContentController:(WKUserContentController
*)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@“MyEvent”]) {
// message.body is the Obj-C object
}
}
Demo
https://github.com/rajagp/iOS-WKWebViewBridgeExample-ObjC
https://github.com/rajagp/iOS-WKWebViewBridgeExample-Swift
Thank you!
@rajagp

Native-Javascript Bridging Using WKWebViews in iOS8